This is a Java Program that Changes the Look and Feel of the Component
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.
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:
When the Motif Look and Feel is selected, then it is expected that frame has the Motif/Linux API.
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.
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.
/*Java Program to Change Look and Feel*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
class Look_Feel implements ItemListener
{
JFrame frame;
JLabel label;
JRadioButton radio[];
UIManager.LookAndFeelInfo looks[]=UIManager.getInstalledLookAndFeels();
String UI_Names[]={"Metal","Windows","Motif"};
//Constructor to create the components
Look_Feel()
{
//Create a frame
frame=new JFrame("Change Look & Feel");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(null);
//Create a label
label=new JLabel();
label.setBounds(150,50,500,50);
frame.add(label);
//Create 3 buttons
ButtonGroup group=new ButtonGroup();
radio=new JRadioButton[3];
for(int i=0;i<3;i++)
{
radio[i]=new JRadioButton(UI_Names[i]);
radio[i].addItemListener(this);
radio[i].setBounds(i*200,250,100,50);
group.add(radio[i]);
frame.add(radio[i]);
}
//Display the default UI
label.setText("This is Metal Look & Feel");
radio[0].setSelected(true);
//Display the frame
frame.setVisible(true);
}
//Function to change the Look and Feel
public void changeLookAndFeel(int index)
{
try
{
label.setText("This is "+UI_Names[index]+" Look & Feel");
UIManager.setLookAndFeel(looks[index].getClassName());
SwingUtilities.updateComponentTreeUI(frame);
}
catch (Exception e)
{
System.out.println("Error. UI Not Found in System");
}
}
//Function to get the button selected
public void itemStateChanged(ItemEvent e)
{
for(int i=0;i<3;i++)
{
if(radio[i].isSelected())
{
changeLookAndFeel(i);
break;
}
}
}
//Driver function
public static void main(String args[])
{
Look_Feel obj=new Look_Feel();
}
}
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.
Here’s the run time test cases to change the Look and Feel of the Component.
Test case 1 – For the Metal Look and Feel
Test case 2 – For the Windows Look and Feel
Test case 3 – For the Motif Look and Feel
Sanfoundry Global Education & Learning Series – Java Programs.
- Check Java Books
- Practice BCA MCQs
- Check Programming Books
- Practice Programming MCQs
- Practice Information Technology MCQs