Home > Software design >  AWT and Swing components not rendering properly
AWT and Swing components not rendering properly

Time:10-28

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.

Not correctly rendered frame, the lowest Panel should look like the ones above, but it's almost unpopulated

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);

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)

CodePudding user response:

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.

I.E. 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.

  • Related