Monday, April 29, 2013

Change Netbeans Java Swing Applications Interface Look-and-Feel to System Native Look

For some or other reason people at Oracle/Netbeans want us to use the horrendous (IMHO) Nimbus theme.

You can change that quite easily, this is one of the first things I do.

Alter your main class (don't put an interface on it), so look like the snippet below, which also shows you how to add an icon to your application.

Put the icon.png file in the same folder as the source, this Init class will call the MainForm (or whatever) and show it with the specified icon:
public class Init {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        setLookAndFeel();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MainForm m = new MainForm();
                m.setTitle("EncodingConverter");
                m.setIconImage(new ImageIcon(getClass().getResource("icon.png")).getImage());
                m.setVisible(true);
            }
        });
    }
    public static void setLookAndFeel() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.out.println("Error setting native LAF: " + e);
        }
    }
}

This does not however make the icon show on Windows, so add this: Right-click Form, Properties, set 'iconImages' to 'none', click 'iconImage'->'Value from existing component', click 'Property' then '...' then 'iconImage'

Now your Java application should look pretty much like other applications that run on the same OS, and yes, I WANT it to look like the other applications. :-)


No comments:

Post a Comment