0

In a "Battleship" program I wrote, I included the possibility to change the "look and feel" of a program to SystemDefault, "Metal" (Java Default) or "Motif" (also included in Java). If you choose the desired LAF from a JComboBox (--> changes a predefined String, lookFeel) and press a confirm button, UIManager.setLookAndFeel(lookFeel) is called, and also SwingUtilities.updateComponentTreeUI(this) and the method updateUI() with the following code:

public static void updateUI() {
        //SwingUtilities.updateComponentTreeUI();
        SwingUtilities.updateComponentTreeUI(colorChooser);
        SwingUtilities.updateComponentTreeUI(shipChooser);
        SwingUtilities.updateComponentTreeUI(guide);
        SwingUtilities.updateComponentTreeUI(menu_bar);
        SwingUtilities.updateComponentTreeUI(menu_general);
        SwingUtilities.updateComponentTreeUI(menu_customization);
        SwingUtilities.updateComponentTreeUI(lafChooser);

        SwingUtilities.updateComponentTreeUI(LAN.lanframe);
        SwingUtilities.updateComponentTreeUI(LAN.hostframe);
        SwingUtilities.updateComponentTreeUI(LAN.joinframe);
    }

However, this code will not update other windows that are part of a different class. Hence, if I launch my main program, and then change the look&feel, these windows won't be affected. Those classes that actually create a frame are not a problem, but my main program just extends JFrame (something I probably wouldn't do again, but I was entirely new to Java when I started writing this program). Now my question is: how can I change the LAF of this class? Thanks in advance!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
PixelMaster
  • 895
  • 10
  • 28
  • Is problem about the changing of the LAF of your main frame which is class extended from JFrame? – STaefi Feb 07 '16 at 14:35
  • 1
    Possible duplicate of [Java look and feel at runtime](http://stackoverflow.com/questions/8953935/java-look-and-feel-at-runtime) – STaefi Feb 07 '16 at 14:37
  • 1
    by default JFrame (or another top ancessor) is target for, e.g. `SwingUtilities.updateComponentTreeUI(myLocalVariableForJFrame);` – mKorbel Feb 07 '16 at 15:05
  • considering that i tried and failed too often for my taste getting this right, I've just decided to change my main class - it now creates an instance of JFrame, instead of extending JFrame. i guess extending JFrame instead of creating an instance is probably dumb anyways ^^ – PixelMaster Feb 07 '16 at 17:05

2 Answers2

3

If the result of getSupportsWindowDecorations() is true for a given LookAndFeel, you can invoke setWindowDecorationStyle() on the JRootPane. A complete example is cited here in UIManager Defaults.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

See How to change swing applicaiton's look and feel at runtime? on how to call updateComponentTreeUI on all windows of your application.

Note that the updateComponentTreeUI will recursively update all subcomponents, therefore you don't need to call it for each and every component, rather call it for all the windows (frames).

Community
  • 1
  • 1
Gee Bee
  • 1,794
  • 15
  • 17
  • well, it didn't work if I called it only on the frame (maybe I should have called it on the root pane or whatever), but anyhow, I've already changed my program structure so that my main class no longer extends JFrame. I also wrote that in a comment below my question, but I guess you missed that. Thank you nevertheless! – PixelMaster Feb 24 '16 at 19:29
  • Oh, one thing more: you can't update the outer LAF from native to non-native properly of a frame which is already rendered on the screen. This is a Swing limitation. You need to close the frame, change the LAF, and open it again. The updateComponentTreeUI works well for the components inside a frame, and - please check - switching between non-native LAFs, e.g. switching between variants of Metal LAF. – Gee Bee Feb 25 '16 at 12:02