简体   繁体   中英

AWT and Swing components not rendering properly

I have an application that is used in our company to search the server for files relating to customers (the search provided by macOS fails and even third part programs such as FoxTrotSearch fail to find all the files on the network drive).

Sometimes (maybe 10% of times), the Preference Frame is not rendered completely. Some of the Components are simply missing on screen, but are somehow present because the other ones are placed correctly around the empty space. The missing components appear if I select another window/frame/application.

没有正确渲染帧,最低的面板应该看起来像上面的那些,但它几乎没有填充

I've noted, that the problem appears more often when there are more components involved. Sometimes only a few components are missing, sometimes close to 50%.

I place and add all the components before calling validate(); pack(); setVisible(true); validate(); pack(); setVisible(true);

Is there something I am missing or is this a behaviour that just has to be accepted?

For completeness: I am using following components:

  • java.awt.Component
  • java.awt.Label
  • java.awt.TextField
  • javax.swing.JCheckBox
  • javax.swing.JFrame
  • javax.swing.JSeparator
  • javax.swing.JSpinner
  • ( java.awt.FlowLayout )
  • ( javax.swing.BoxLayout )

Don't mix Swing and AWT components. Mixing them together might (often does) cause problems due to Swing providing lightweight components, while AWT has heavyweight components.

IE Instead of Component , Label & TextField use JComponent (or JPanel ), JLabel & JTextField .

Note: Swing is built on AWT components - the inheritance hierarchy of Swing components typically leads back to an AWT component eventually. Also Swing uses a great many AWT APIs (printing, Java2D fonts, ..) and many of the AWT based layouts. It's just the components we need to be careful with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM