Programatically Loading Themes
I'm using the recommended approach of using .properties files I'd like to provide my users with the option of defining their own themes by providing the same files which my app would load at runtime. Is that possible ? I couldn't figure out a way of dynamically loading those files at runtime.
Yea had a similar idea.
Currently a change in the properties file requires a restart of the application to be applied. It would be great if we could simply reload the properties + a refresh like during a L&F change (light to dark) to see the changes.
It would improve the design process for colors and other properties.
And as @MerlinUKRhapsody says also would allow this for the user. I also can think of a settings panel where some parameters can be changed by the user and will be applied to the properties file or a layer in between.
I'd like to provide my users with the option of defining their own themes by providing the same files which my app would load at runtime. Is that possible?
@MerlinUKRhapsody yes, use class FlatPropertiesLaf to do that. E.g.
FlatLaf.setup( new FlatPropertiesLaf( "My Theme," new File( "some-path-to/MyTheme.properties" ) ) );
Currently a change in the properties file requires a restart of the application to be applied. It would be great if we could simply reload the properties + a refresh like during a L&F change (light to dark) to see the changes.
@MrUrdam to reload a theme, simply invoke FlatLaf.setup(...) (or UIManager.setLookAndFeel()) followed by FlatLaf.updateUI();. E.g.:
FlatLaf.setup( new FlatPropertiesLaf( "My Theme," new File( "some-path-to/MyTheme.properties" ) ) );
FlatLaf.updateUI();
You could use listener WindowListener.windowActivated() on your main window to check whether time stamp of MyTheme.properties has changed and then reload it. Then you can edit the properties file in any editor, save it and as soon as you switch to your app window it gets reloaded.
@DevCharly thank you for this tip and guide. But I think this does not fully catch my problem. I have several LaFs in different packages that rely on each other by extending.
For example
public class UrdamSpecialLight extends UrdamLight {
public static boolean setup() {
return FlatLaf.setup(new UrdamSpecialLight());
}
}
with two different property files UrdamSpecialLight.properties and UrdamLight.properties
I now would like to load the UrdamSpecialLight theme from disk and in setup() I can add a case that uses your example code with FlatPropertiesLaf.
But there I am limited to a single file and in extend to your predefined @baseTheme. But I am not able to combine my two LaFs like doing in the „normal“ process.
Any tips on that? :)
@MrUrdam for reloading themes after changes in development, FlatPropertiesLaf is not the right thing.
Better use something like:
UrdamSpecialLight.setup();
FlatLaf.updateUI();
You need to detect somehow, whether a properties file has changed. FlatLaf Demo does this in a window activated event. Maybe you can use some code:
https://github.com/JFormDesigner/FlatLaf/blob/babc8aa55d7d20b77f871f5046308a4ea03da217/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/intellijthemes/IJThemesPanel.java#L424-L442