Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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. 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.

                         

» View all related tutorials
Related Tags: c memory image class images interface io colors sed color get implementation display icon height value source width int id

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.