How to apply/set up swing look and feel?

UPDATED: 20 June 2013
Swing application allows us to change the Look and Feel of the frame and its internal components as well. UIManager.setLookAndFeel() method used with different arguments to change its look.

Swing look and feel

We'll achieve blow swing Look and Feel
  • Metal - javax.swing.plaf.metal.MetalLookAndFeel
  • Nimbus - com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel
  • CDE/Motif - com.sun.java.swing.plaf.motif.MotifLookAndFeel
  • Windows - com.sun.java.swing.plaf.windows.WindowsLookAndFeel
  • Windows Classic - com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel

To achieve Look and Feel add the code before it get visible. You have to copy just try-catch block in below code. If you set look and feel to parent Jframe. It also affect look to the children (calling other frame). Check the below code snippet. You have to uncomment the line for your desired look.
public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                /* It represent the frame border as internal jframe.
                 * However it only works with metal look and feel.
                 */
                //JFrame.setDefaultLookAndFeelDecorateld(true); 
                try {
                   //UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                   UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                   //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(LookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InstantiationException ex) {
                    Logger.getLogger(LookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(LookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
                } catch (UnsupportedLookAndFeelException ex) {
                    Logger.getLogger(LookAndFeel.class.getName()).log(Level.SEVERE, null, ex);
                }
                new LookAndFeel().setVisible(true);
            }
        });
}

0 comments :