Java Look and Feel

Beginner java tutorial site is rehosted with updated content and useful information under the domain name javabeginner.com

Java Look and Feel

 

Java Swing Tutorial Explaining the Java look and feel. Java Swing api provides pluggable look and feel ( PL&F) capability that allows swing gui widgets to change appearance based on the programmers customized look and feel setting.

 

 

Each Java runtime has a UIManager object that determines the look and feel of the Swing components. Below are some example ode snippets to implement the java look and feel your java application.

 

UIManager.setLookAndFeel ("java.awt.swing.plaf.metal.MetalLookAndFeel" ) ;
SwingUtilities.updateComponentTreeUI ( this ) ;

UIManager.setLookAndFeel ("java.awt.swing.plaf.windows.WindowsLookAndFeel" ) ;
SwingUtilities.updateComponentTreeUI ( this ) ;

UIManager.setLookAndFeel ("java.awt.swing.plaf.motif.MotifLookAndFeel" ) ;
SwingUtilities.updateComponentTreeUI ( this ) ;

Each platform has a system-default look and feel. This source code shows how to establish the system default look and feel.

UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
SwingUtilities.updateComponentTreeUI( this );

Java look and feel Source Code

1. To determine the installed look and feel in your system

      
//Get Installed look and Feels

import javax.swing.UIManager;

public class InstalledLookandFeels {
  public static void main(String args[]) {
    UIManager.LookAndFeelInfo laf[] = UIManager.getInstalledLookAndFeels();
    for (int i = 0, n = laf.length; i < n; i++) {
      System.out.print("LAF Name: " + laf[i].getName()+"\t");
      System.out.println("  LAF Class name: " + laf[i].getClassName());
    }
    System.exit(0);
  }
}


        

 

Output

LAF Name: Metal LAF Class name: javax.swing.plaf.metal.MetalLookAndFeel
LAF Name: CDE/Motif LAF Class name: com.sun.java.swing.plaf.motif.MotifLookAndFeel
LAF Name: Windows LAF Class name: com.sun.java.swing.plaf.windows.WindowsLookAndFeel

Download Installed Look and Feels Source Code

2. To change the look and feel in your gui by clicking on buttons

      
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class LookAndFeel1 extends JPanel implements ActionListener {

	private JButton jbmMetal = new JButton("Metal");
	private JButton jbnMotif = new JButton("Motif");
	private JButton jbnWindows = new JButton("Windows");

	public LookAndFeel1() {

	    add(jbmMetal);
	    add(jbnMotif);
	    add(jbnWindows);
	
	    jbmMetal.addActionListener(this);
	    jbnMotif.addActionListener(this);
	    jbnWindows.addActionListener(this);
	}

	  public void actionPerformed(ActionEvent e) {
		  Object source = e.getSource();
		  String laf = "";
		  if (source == jbmMetal)
			  laf = "javax.swing.plaf.metal.MetalLookAndFeel";
		  else if (source == jbnMotif)
			  laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
		  else if (source == jbnWindows)
			  laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
		  try {
			  		UIManager.setLookAndFeel(laf);
			  		SwingUtilities.updateComponentTreeUI(this);
		  	  } catch (Exception excep) {
		  	  }
	  }

  public static void main(String[] args) {
	    JFrame frame = new JFrame();
	    frame.setTitle("Look and Feel Test");
	    frame.setSize(300, 200);
	    frame.addWindowListener(new WindowAdapter() {
	      public void windowClosing(WindowEvent e) {
	        System.exit(0);
	      }
	    });
	
	    Container contentPane = frame.getContentPane();
	    contentPane.add(new LookAndFeel1());
	
	    frame.setVisible(true);
  }
}



        

 

Output

Metal LAF

Motif LAF

Windows LAF

Download Look and Feel Source Code

3. To change the look and feel in your gui by selection the Radio Buttons.

Download Look and Feel Source Code