Runtime Look and Feel Switching

Following on from my earlier Options Window Color Analysis, here's the Search field in the Options window, also darkened, important for air traffic systems that need to avoid the white glare of default UI text fields.

The key to this is this:

UIManager.put("TextField.background", Color.LIGHT_GRAY);

Below is code for a runtime look and feel swither.

@OnShowing
@ActionID(category = "Switcher", id = "org.m1.Startable")
@ActionRegistration(displayName = "Switcher")
@ActionReference(path = "Toolbars/Switcher")
public class Startable extends AbstractAction implements Runnable, Presenter.Toolbar {
    @Override
    public void run() {
        switchLookAndFeel("Nimbus");
    }
    public void switchLookAndFeel(String laf) {
        if (laf.equals("Nimbus")) {
            try {
                UIManager.setLookAndFeel(new NimbusLookAndFeel());
            } catch (UnsupportedLookAndFeelException ex) {
            }
        } else {
            try {
                UIManager.setLookAndFeel(new MetalLookAndFeel());
            } catch (UnsupportedLookAndFeelException ex) {
            }
        }
        UIManager.put("Tree.background", Color.LIGHT_GRAY);
        UIManager.put("TextField.background", Color.LIGHT_GRAY);
        applyUIChanges(null);
    }
    void applyUIChanges(Object component) {
        if (component instanceof Component) {
            SwingUtilities.updateComponentTreeUI((Component) component);
        } else if (component == null) {
            Window windows[] = Window.getWindows();
            for (Window window : windows) {
                if (window.isDisplayable()) {
                    applyUIChanges(window);
                }
            }
        }
    }
    @Override
    public Component getToolbarPresenter() {
        JPanel jp = new JPanel();
        jp.setLayout(new BoxLayout(jp, BoxLayout.X_AXIS));
        ButtonGroup bg = new ButtonGroup();
        JCheckBox metal = new JCheckBox("Metal");
        metal.addActionListener(new SwitchActionListener("Metal"));
        JCheckBox nimbus = new JCheckBox("Nimbus");
        nimbus.addActionListener(new SwitchActionListener("Nimbus"));
        bg.add(metal);
        bg.add(nimbus);
        jp.add(metal);
        jp.add(nimbus);
        return jp;
    }
    private class SwitchActionListener implements ActionListener {
        private final String laf;
        private SwitchActionListener(String laf) {
            this.laf = laf;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if (laf.equals("Metal")) {
                switchLookAndFeel("Metal");
            } else {
                switchLookAndFeel("Nimbus");
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
    }
}

Comments:

Just for information

http://plugins.netbeans.org/plugin/49218/

Posted by K Adithyan on March 04, 2014 at 12:23 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2015
SunMonTueWedThuFriSat
   
4
5
11
12
25
26
27
28
29
30
  
       
Today