简体   繁体   中英

Swing How do I allow a User-Selected Theme?

I'm trying to allow users to change the applications theme like dark/light etc.

To do this so far I've been using:

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if (newTheme.equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        SwingUtilities.updateComponentTreeUI(getRootPane());
                    }
                });


                break;
            }
        }
    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
    }

I'm not sure I really should even be changing the entire look and feel. The problem with using this method, is I can see no way of setting a jPanel to anything that would change with the theme.

An example of what I mean is something like setting: jPanel 1 to color: menu jpanel 2 to color: background

Where in theme 1, then menu may be grey, and background may be blue. In theme 2 menu would be green and background red as an example

Have you tried implementing SystemColor objects? SystemColors can change. Their red/green/blue values are tied to the underlying OS theme. If the user changes their OS theme's text highlight color, for instance, then SystemColor.textHighlight changes along with it. On the next redraw, any Java component that uses that SystemColor object redraws with the user's updated color.

This feature of SystemColors allows Java applications to draw user interfaces with color schemes that track the user's own OS color scheme preferences. Which for most cases such as yours, no special application coding is needed. Hope this helps!

  • Robin.

As @madprogrammer already mentioned in a comment - it is generally not recommended to change look and feel (L&F) in runtime as it might cause a lot of unwanted behaviors and potentially issues.

To be more specific - different L&Fs will have different size of components, spacings, borders and just overall visual style (unless you are using the same L&F but with different themes/skins). That will make your application UI look and perform inconsistently on those different L&Fs in many cases, especially if all you want is to have dark & light themes but the same UI layout/style. Changing L&F in runtime might also reset the state of some components to some extent which could potentially lead to logical errors in your application.

Better approach is the use of a single L&F that supports dark and light (or any other) themes/skins as there will surely be way less issues if you only change between different implementations/configurations of the very same L&F as the L&F developer most probably accounted for that possibility. You can find a comprehensive list of currently available L&Fs on the other SO topic here: Java Look and Feel (L&F)

Although writing your own L&F could also be a good solution if you are ready to invest into it. But don't be mistaken, it's not an easy task and will take a lot of time and Swing knowledge to create one. Swing and L&F API in particular is quite poorly documented and original developer didn't provide a lot of tips about how you should make one. You can only go the path of trial and error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM