how to change java swing look and feel without application restart?
Asked Answered
P

1

7

is there a specific way to change the look and feel of a swing application without needing to restart the application every time the LAF got changed by the user?

im looking for a solution where one can select the LAF from a configuration dialog and it changes directly on apply without having to fall back to telling the user to restart the application.

currently i remember the selected LAF in a properties file and set it on startup before any window has been opened.

Peer answered 22/9, 2010 at 10:3 Comment(5)
Does this not work? #2656429Bairn
@Bairn Actually sometimes it doesn't. Listeners in particular will be fired in a different order, which shouldn't matter but often does. Also I wouldn't have total faith in PL&F uninstalls (notably if UIResource isn't used (as it can't with some classes and primitives), then how can the PL&F accurately detect whether the application code has an interest in a component property?).Cerebrate
@Tom But it might work if it's not too complex an application, and it's the only hope of a working solution other than going for the full restart, which the OP wants to avoid...Bairn
@Bairn It might well work, but I wouldn't want to rely on the next update leaving egg on my face. If it's a well written program(!), it shouldn't be too difficult to clear the UI, change the PL&F and recreate the UI without too much changing.Cerebrate
@Bairn also if custom colors and fonts are set, then these values should be reset in updateUI() method. But in general it should work. You can change L&F in IntelliJ IDEA without restarting and I wouldn't say it's a small app.Stringendo
G
5

From the Java tutorial:

You first need to set the new look and feel by calling UIManager.setLookAndFeel:

UIManager.setLookAndFeel(lnfName);

Then, in order to make existing componentes reflect the new look and feel, call the SwingUtilities.updateComponentTreeUI method on each top-level container (dialogs, frames..). You might also wish to resize each top-level container to adapt to changes in the size of its contained components:

SwingUtilities.updateComponentTreeUI(frame);  // update components
frame.pack();                                 // adapt container size if required
Gnathion answered 22/9, 2010 at 12:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.