Changing Look and Feel of Swing Application

This section shows how to set different look and feel for your Swing Application.

Changing Look and Feel of Swing Application

This section shows how to set different look and feel for your Swing Application.

Changing Look and Feel of Swing Application

Changing Look and Feel of Swing Application

     

This section shows how to set different look and feel for your Swing Application. The look and feel feature of Java Swing provides more interactivity of the frame for user application. Swing allows to change the Look and Feel of the application on the fly.

In this section, one program has been given to illustrate this feature. This program displays a text area with three command buttons on the frame. Every button labeled with a unique look and feel name. When you click on the specific button, look of the frame will be changed according to the specified look and feel which name is mentioned on the button and the class name of the applied look and feel is shown in the text are.

Screen shots for the result of the given program are given below:

When you run the program:
First time seen frame for showing look and feel

This frame shows the "Metal" look and feel when you click on the "Metal" button.
Metal look and feel.

This frame shows the "Motif" look and feel when you click on the "CDE/Motif" button.
Motif look and feel.

This frame shows the "Window" look and feel when you click on the "Window" button.
Window look and feel.

This program helps you for how to get the look and feel for the frame and set it to the specified frame using some following methods and APIs are given as follows:

UIManager.LookAndFeelInfo:
This is the nested class of UIManager class. The object of this class returns the information about the installed look and feels with the available software development kit. This class creates the instance using the getInstalledLookAndFeels() method of the UIManager class.

LookAndFeel.getName():
This method returns the brief name of the specified look and feel. For example, name of the "javax.swing.plaf.metal.MetalLookAndFeel" look and feel is simply "Metal".

LookAndFeel.getClassName():
Above method returns the class name for the look and feel of the frame. For example, class name of the "Metal" look and feel is "javax.swing.plaf.metal.MetalLookAndFeel" which is used to set the look and feel.

UIManager.setLookAndFeel(lookAndFeelClassName):
Above method sets the looks of the frame by the specified look and feel class name i.e. passed through the method as a parameter.

SwingUtilities.updateComponentTreeUI(frame):
Above method update the each and every node of the frame automatically with the changed look and feel.

Here is the code of the program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GettingAndSettingLAF {
  JFrame frame;
  JTextArea txtArea;
  public static void main(String args[]) {
  GettingAndSettingLAF mc = new GettingAndSettingLAF();
  }

  public GettingAndSettingLAF(){
  frame = new JFrame("Change Look");
  UIManager.LookAndFeelInfo lookAndFeels[]

 
= UIManager.getInstalledLookAndFeels();
  JPanel panel = new JPanel();
  JPanel panel1 = new JPanel();
  txtArea = new JTextArea(515);
  JScrollPane sr = new JScrollPane(txtArea);
  panel1.add(sr);
  for(int i = 0; i < lookAndFeels.length; i++){
  JButton button = new JButton(lookAndFeels[i].getName());
  button.addActionListener(new MyAction());
  panel.add(button);
  }
  frame.add(panel1,BorderLayout.NORTH);
  frame.add(panel, BorderLayout.CENTER);
  frame.setSize(300200);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setResizable(false);
  frame.setVisible(true);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent ae){
  Object EventSource = ae.getSource();
  String lookAndFeelClassName = null;


  UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
  for(int i = 0; i < looks.length; i++){
  if(ae.getActionCommand().equals(looks[i].getName())){
  lookAndFeelClassName = looks[i].getClassName();
  break;
  }
  }
  try{
  UIManager.setLookAndFeel(lookAndFeelClassName);
  txtArea.setText(lookAndFeelClassName);
  SwingUtilities.updateComponentTreeUI(frame);
  }
  catch(Exception e){
  JOptionPane.showMessageDialog(frame, "Can't change look and feel:" 
+ lookAndFeelClassName, 
"Invalid PLAF"
JOptionPane.ERROR_MESSAGE
);
  }
  }
  }
}  

Download this example.