Issue
My java program has multiple classes, each containing a separate JFrame. All three JFrames are started in my main class, and are set to setVisible(false) so they are hidden. When a button from a JPopUpMenu is clicked, the respective JFrame is set to setVisible(true). In these classes I have defined:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
When I run each class separately by right-clicking the .java file in NetBeans and choosing Run File, the Look and Feel works perfectly. However, when I run the main class and setVisible(true) on the JFrame, the look and feel does not work. I thought this may have been just an error with NetBeans, so I tried building the .jar and running it. The problem is still there. The main class's Look and Feel has been set using the same code, and that works perfectly. I am not sure what to do. Any help is appreciated.
Solution
From the JavaDocs of UIManager:
Once the look and feel has been changed it is imperative to invoke
updateUI
on allJComponents
. The methodSwingUtilities.updateComponentTreeUI(java.awt.Component)
makes it easy to applyupdateUI
to a containment hierarchy. Refer to it for details. The exact behavior of not invokingupdateUI
after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.
If you change the look and feel after creating your frames (even if they're not yet visible) then you need to updateUI
for the change to take effect (or better, if you know you're always going to want to use a specific look and feel then set that in your main class before you create the frames in the first place).
Answered By - Ian Roberts
Answer Checked By - Pedro (JavaFixing Volunteer)