devxlogo

Swing, Pluggable Look & Feel

Swing, Pluggable Look & Feel

With Swing, you can easily change the Look & Feel (L&F) at run time to match one of the L&F implementations in your system. This program contains strings identifying three different L&F implementations contained in Swing 1.0.3 for Win95. Compile and run the program to see PL&F in action. The key statements are:

 plafClassName = motif;//or metal or windows  UIManager.setLookAndFeel(plafClassName);SwingUtilities.updateComponentTreeUI(thisPlafPanel);  

To change the L&F, assign a different L&F string to plafClassName and recompile the program. To upgrade the program to change the L&F at run time, assign a different L&F sting to plafClassName and execute the other two key statements. This code was tested using JDK1.1.6 and Swing 1.0.3 under Win95.

 import java.awt.event.*;import java.awt.*;import com.sun.java.swing.*;import java.util.*;public class PlafStandalone01 extends JFrame {	//Three L&F implementations contained in Swing 1.0.3	// for Win95 follow.	String metal = 		"com.sun.java.swing.plaf.metal.MetalLookAndFeel";	String motif = 			"com.sun.java.swing.plaf.motif.MotifLookAndFeel";	String windows = 		"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";	String plafClassName;//store the L&F string here	PlafStandalone01 thisPlafPanel = this;//ref to this obj	//-----------------------------------------------------//  	public PlafStandalone01(){//constructor		getContentPane().setLayout(new FlowLayout());		getContentPane().add(new JButton("JButton"));		getContentPane().add(new JToggleButton(				"JToggleButton"));		setTitle("Pluggable Look & Feel");		setBounds(30,300,350,150);    		//Set the Look and Feel by enabling one of these		//plafClassName = motif;		//plafClassName = metal;		plafClassName = windows;		try{			UIManager.setLookAndFeel(plafClassName);		}catch(Exception ex){System.out.println(ex);}      		//Cause the L&F to become visible.		SwingUtilities.updateComponentTreeUI(thisPlafPanel);		this.setVisible(true);	}//end constructor	//=====================================================//	public static void main(String[] args){		new PlafStandalone01();	}//end main()}//end class PlafStandalone01

devx-admin

Share the Post: