User Interface(UI) In Java | Free Source Code

User Interface(UI) in Java

May 30, 2020
Tutorials
ui design in java

In this tutorial will teach you how to make a program that can choose and change the User Interface(UI) that is already installed in java.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of UILookAndFeel.java.

2. Import the following package library:

import java.awt.event.*; //used to access the ActionEvent and ActionListener class
import java.awt.*; //used to access the GridLayout class
import javax.swing.*; //used to access the JButton,JList,JTextArea,JFrame,JPanel,SwingUtilities and UIManager class

3. Initialize the following variable components in your Main.

final JFrame frame = new JFrame("Change User Interface");
JList comboBox = new JList(new String[] { "www.campcodes.com", "This is a List." });
JTextArea txtArea = new JTextArea("www.facebook.com/BermzISware \n This is a TextArea.");
JPanel panel = new JPanel();

4. To have an event for changing the UI of the frame, we will use the ActionListener and when clicking a particular component it will change the UI directly.

ActionListener changeUI = new ActionListener() {
  public void actionPerformed(ActionEvent actionEvent) {
    String plaf = null;
    plaf = actionEvent.getActionCommand();
    String finalLafClassName = plaf;
    try {
      UIManager.setLookAndFeel(finalLafClassName);
      SwingUtilities.updateComponentTreeUI(frame);
    } catch (Exception exception) {
     System.out.print("Can't change look and feel.");
    }
 
  }
};

To get all the installed User Interface in java, have this code below:

UIManager.LookAndFeelInfo UIlooks[] = UIManager.getInstalledLookAndFeels();

5. Create a JButton component to get all the installed UI in your java software. This will also call the ActionListener that we have declared above when clicking the button.

for (int i = 0, n = UIlooks.length; i < n; i++) {
  JButton button = new JButton(UIlooks[i].getName());
  button.setActionCommand(UIlooks[i].getClassName());
  button.addActionListener(changeUI);
  panel.add(button);
}

6. Finally, add all the components, set the layout into GridLayout, set the size, visibility, and the close operation of the frame. Copy this code below:

 frame.getContentPane().setLayout(new GridLayout(1,1));
 frame.getContentPane().add(comboBox);
 frame.getContentPane().add(txtArea);
 frame.getContentPane().add(panel);
 frame.setSize(580, 250);
 frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


Output:

Metal Design

metal ui design in java

CDE/Motif Design:

motif ui in java

Windows Design:

windows design in java

Here’s the full code of this tutorial:

import java.awt.event.*; //used to access the ActionEvent and ActionListener class
import java.awt.*; //used to access the GridLayout class
import javax.swing.*; //used to access the JButton,JList,JTextArea,JFrame,JPanel,SwingUtilities and UIManager class
 
public class UILookAndFeel {
 
  public static void main(String args[]) {
    final JFrame frame = new JFrame("Change User Interface");
    JList comboBox = new JList(new String[] { "www.campcodes.com", "This is a List." });
    JTextArea txtArea = new JTextArea("www.facebook.com/BermzISware \n This is a TextArea.");
    JPanel panel = new JPanel();
 
 
    ActionListener changeUI = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        String plaf = null;
        plaf = actionEvent.getActionCommand();
        String finalLafClassName = plaf;
        try {
          UIManager.setLookAndFeel(finalLafClassName);
          SwingUtilities.updateComponentTreeUI(frame);
        } catch (Exception exception) {
         System.out.print("Can't change look and feel.");
        }
 
      }
    };
 
    UIManager.LookAndFeelInfo UIlooks[] = UIManager.getInstalledLookAndFeels();
 
 
    for (int i = 0, n = UIlooks.length; i < n; i++) {
      JButton button = new JButton(UIlooks[i].getName());
      button.setActionCommand(UIlooks[i].getClassName());
      button.addActionListener(changeUI);
      panel.add(button);
    }
 
    frame.getContentPane().setLayout(new GridLayout(1,1));
    frame.getContentPane().add(comboBox);
    frame.getContentPane().add(txtArea);
    frame.getContentPane().add(panel);
    frame.setSize(580, 250);
    frame.setVisible(true);
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.