NetBeans Platform: How to style the NetBeans GUI components programmatically?

ⅰ亾dé卋堺 提交于 2021-02-08 11:28:26

问题


Intro

I have to program a fat client using NetBeans Platform and JavaFX 11. One of the requirements is to offer a default and dark theme mode for the application. So if the user of my GUI application press the dark mode button, the application's GUI changes to the dark style.

Problem

While its obvious for me how to style the JFX components (via a CSS file), the CSS file doesn't has an impact on the NetBeans Platform's components like the menu bar. For instance the JavaFX components inside a scene object got a dark styling, but the NetBeans menu bar is still light.

Question

How its possible to change the style of NetBeans Platform's components programmatically (just as you would have done with CSS in web development or with the CSS support in JavaFX)?


回答1:


There is an embedded dark LAF in the platform (at least in NB12 which I use) called FlatDarkLaf, it looks nice.

In order to use it, you need to add the following code in the validate() method of a ModuleInstall subclass, so that it's done very early during the startup process.

NbPreferences.root().node("laf").put("laf", "com.formdev.flatlaf.FlatDarkLaf");
UIManager.installLookAndFeel(new UIManager.LookAndFeelInfo("FlatLaf Dark", latDarkLaf.class.getName()));  

To switch back to the default theme:

NbPreferences.root().node("laf").remove("laf");

I'm not aware of a (simple) way of changing the LAF without restarting.

For a more complete sample code have a look at my application JJazzLab-X on GitHub, in the UISettings Netbeans module.




回答2:


For the sake of completeness I add the code fragment that solved my problem:

    private void toggleLookAndFeel() {
        final boolean darkModeEnabled = UIManager.getLookAndFeel().getName().equals("Flat Dark");
        final String darkLookAndFeel = "com.formdev.flatlaf.FlatDarkLaf";
        final String defaultLookAndFeel = UIManager.getSystemLookAndFeelClassName();
        try {
            if (darkModeEnabled) {
                // set laf of swing components
                UIManager.setLookAndFeel(defaultLookAndFeel);
                // use default css file for fx components
                this.fxPanel.getScene().getStylesheets().remove(0);
            } else {
                // Sets laf of swing components:
                UIManager.setLookAndFeel(darkLookAndFeel);
                // use app.css file for fx components
                this.fxPanel.getScene().getStylesheets().add(0, getClass().getResource("app.css").toExternalForm());
            }
        } catch (IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        triggerSwingComponentUpdate();
    }

    private static void triggerSwingComponentUpdate() {
        SwingUtilities.invokeLater(() -> {
            for (final Window w : Window.getWindows()) {
                SwingUtilities.updateComponentTreeUI(w);
            }
        });
    }


来源:https://stackoverflow.com/questions/65698853/netbeans-platform-how-to-style-the-netbeans-gui-components-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!