Quantcast
Channel: Oracle Bloggers
Viewing all articles
Browse latest Browse all 19780

Runtime Look and Feel Switching

$
0
0
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) {
    }
}


Viewing all articles
Browse latest Browse all 19780

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>