Java Program that Changes the Look and Feel of the Component

This is a Java Program that Changes the Look and Feel of the Component

Problem Description

We have to write a program in Java such that it allows the user to switch between three Look and Feel – Metal, Windows and Motif using radio buttons.

Expected Input and Output

To switch the Look and Feel of the Component, we have the following sets of input and output.

1. Metal Look and Feel:

When the Metal Look and Feel is selected,
then it is expected that frame has the default Java API.

2. Windows Look and Feel:

When the Windows Look and Feel is selected,
then it is expected that frame has the Windows System API.

3. Motif Look and Feel:

advertisement
advertisement
When the Motif Look and Feel is selected,
then it is expected that frame has the Motif/Linux API.
Problem Solution

1. Define a constructor of the class to create the components of the frame as required.
2. Change the Look and Feel of the component based on the option selected.
a) Metal: This is the default API of the Java framework.
b) Windows: This is the Windows System API.
c) Motif: This is the Linux System API for systems with GTK+ 2.2 or later.

3. Update the Look & Feel of the components of frame.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Program/Source Code

Here is source code of the Java Program to change Look and Feel of Components. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

  1. /*Java Program to Change Look and Feel*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.SwingUtilities;
  6. import javax.swing.UIManager;
  7.  
  8. class Look_Feel implements ItemListener
  9. {
  10.     JFrame frame;
  11.     JLabel label;
  12.     JRadioButton radio[];
  13.     UIManager.LookAndFeelInfo looks[]=UIManager.getInstalledLookAndFeels();
  14.     String UI_Names[]={"Metal","Windows","Motif"};
  15.     //Constructor to create the components
  16.     Look_Feel()
  17.     {
  18.         //Create a frame
  19. 	frame=new JFrame("Change Look & Feel");
  20. 	frame.setSize(500,500);
  21. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  22. 	frame.setLayout(null);
  23. 	//Create a label
  24. 	label=new JLabel();
  25. 	label.setBounds(150,50,500,50);	
  26. 	frame.add(label);
  27. 	//Create 3 buttons
  28. 	ButtonGroup group=new ButtonGroup();
  29. 	radio=new JRadioButton[3];
  30. 	for(int i=0;i<3;i++)
  31. 	{
  32. 	    radio[i]=new JRadioButton(UI_Names[i]);
  33. 	    radio[i].addItemListener(this);
  34. 	    radio[i].setBounds(i*200,250,100,50);
  35. 	    group.add(radio[i]);
  36. 	    frame.add(radio[i]);
  37. 	}
  38. 	//Display the default UI
  39. 	label.setText("This is Metal Look & Feel");
  40. 	radio[0].setSelected(true);
  41. 	//Display the frame
  42.         frame.setVisible(true);
  43.     }
  44.     //Function to change the Look and Feel
  45.     public void changeLookAndFeel(int index)
  46.     {
  47.         try
  48. 	{
  49. 	    label.setText("This is "+UI_Names[index]+" Look & Feel");
  50. 	    UIManager.setLookAndFeel(looks[index].getClassName());
  51.     	    SwingUtilities.updateComponentTreeUI(frame);
  52.     	}
  53.     	catch (Exception e)
  54.     	{
  55.     	    System.out.println("Error. UI Not Found in System");
  56.     	}
  57.     }
  58.     //Function to get the button selected
  59.     public void itemStateChanged(ItemEvent e)
  60.     {
  61.         for(int i=0;i<3;i++)
  62. 	{
  63. 	    if(radio[i].isSelected())
  64. 	    {
  65. 	        changeLookAndFeel(i);
  66. 		break;
  67. 	    }
  68. 	}
  69.     }
  70.     //Driver function
  71.     public static void main(String args[])
  72.     {
  73.         Look_Feel obj=new Look_Feel();
  74.     }
  75. }
Program Explanation

1. Get the list of installed Look and Feels using function getInstalledLookAndFeels of the UIManager class.
2. Create the components of the frame as required.
3. Associate ItemListener with the radio buttons.
4. When a radio button is selected, get the index of the look and feel selected and call function changeLookAndFeel to change the look and feel to the selected.
5. Change the look and feel using updateComponentTreeUI of the SwingUtilities class.

Runtime Test Cases

Here’s the run time test cases to change the Look and Feel of the Component.

advertisement

Test case 1 – For the Metal Look and Feel
java-program-look-feel-metal

Test case 2 – For the Windows Look and Feel
java-program-look-feel-windows

Test case 3 – For the Motif Look and Feel
java-program-look-feel-motif

Sanfoundry Global Education & Learning Series – Java Programs.

advertisement

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.