March.15Give your java application a new LookAndFeel
A few days ago I wrote a java demo application for a client. One of his primary requirements was better interface. So I thought I would change the theme of the application. After some googling I found that java supports LookAndFeel for basic interface customization. But the example in sun’s site looked gibberish to me. So after some more googling I came up with a solution.Here is what to do.
We have to call javax.swing.UIManager.getInstalledLookAndFeels() to get an array of installed lookandfeel. Then we set our desired LookAndFeel by calling javax.swing.UIManager.setLookAndFeel(LookAndFeel_ClassName). Then we have to update all our components by calling javax.swing.SwingUtilities.updateComponentTreeUI(this). Then we have to wrap this in a try-catch block. Thats all.
Here is the key portion of the code
private void selectLookAndFeel(int num) { try { javax.swing.UIManager.LookAndFeelInfo[] installedLF = javax.swing.UIManager.getInstalledLookAndFeels(); javax.swing.UIManager.setLookAndFeel(installedLF[num].getClassName()); javax.swing.SwingUtilities.updateComponentTreeUI(this); } catch(Exception e) { javax.swing.JOptionPane.showMessageDialog(rootPane, e.getMessage(), "Error", javax.swing.JOptionPane.ERROR_MESSAGE); } }
Here is some screenshots:
Metal:
Motif:
Windows:
Windows Classic:
Here is the Netbeans project file. Its created on Netbeans 6.0 and Java 6.0 . But I am sure it will work with Java 5.





Leave a Reply