Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  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. 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.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.