© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

Changing Look and Feel of Swing Application


This program is 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. This 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 area.

UIManager.LookAndFeelInfo 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 () 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 () 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) method sets the looks of the frame by the specified look and feel class name i.e. passed through the method as a parameter.

Changing Look and Feel of Swing Application:

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(5, 15); 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(300, 300); 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); } } } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑