Change User Interface(UI) in Java

Language

This tutorial will teach you how to create a program that can choose and change the User Interface(UI) that is installed already 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:
  1. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  2. import java.awt.*; //used to access the GridLayout class
  3. 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.
  1. final JFrame frame = new JFrame("Change User Interface");
  2. JList comboBox = new JList(new String[] { "www.sourcecodester.com", "This is a List." });
  3. JTextArea txtArea = new JTextArea("www.facebook.com/BermzISware \n This is a TextArea.");
  4. 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.
  1. ActionListener changeUI = new ActionListener() {
  2. public void actionPerformed(ActionEvent actionEvent) {
  3. String plaf = null;
  4. plaf = actionEvent.getActionCommand();
  5. String finalLafClassName = plaf;
  6. try {
  7. UIManager.setLookAndFeel(finalLafClassName);
  8. SwingUtilities.updateComponentTreeUI(frame);
  9. } catch (Exception exception) {
  10. System.out.print("Can't change look and feel.");
  11. }
  12.  
  13. }
  14. };
To get all the installed User Interface in java, have this code below:
  1. UIManager.LookAndFeelInfo UIlooks[] = UIManager.getInstalledLookAndFeels();
5. Now, create a JButton component in which it will get all the installed UI in your java software. This will also call the ActionListener that we have declared above when clicking the button.
  1. for (int i = 0, n = UIlooks.length; i < n; i++) {
  2. JButton button = new JButton(UIlooks[i].getName());
  3. button.setActionCommand(UIlooks[i].getClassName());
  4. button.addActionListener(changeUI);
  5. panel.add(button);
  6. }
6. Lastly, add all the components, set the layout into GridLayout, set the size, visibility, and the close operation of the frame. Have this code below:
  1. frame.getContentPane().setLayout(new GridLayout(1,1));
  2. frame.getContentPane().add(comboBox);
  3. frame.getContentPane().add(txtArea);
  4. frame.getContentPane().add(panel);
  5. frame.setSize(580, 250);
  6. frame.setVisible(true);
  7. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.event.*; //used to access the ActionEvent and ActionListener class
  2. import java.awt.*; //used to access the GridLayout class
  3. import javax.swing.*; //used to access the JButton,JList,JTextArea,JFrame,JPanel,SwingUtilities and UIManager class
  4.  
  5. public class UILookAndFeel {
  6.  
  7. public static void main(String args[]) {
  8. final JFrame frame = new JFrame("Change User Interface");
  9. JList comboBox = new JList(new String[] { "www.sourcecodester.com", "This is a List." });
  10. JTextArea txtArea = new JTextArea("www.facebook.com/BermzISware \n This is a TextArea.");
  11. JPanel panel = new JPanel();
  12.  
  13.  
  14. ActionListener changeUI = new ActionListener() {
  15. public void actionPerformed(ActionEvent actionEvent) {
  16. String plaf = null;
  17. plaf = actionEvent.getActionCommand();
  18. String finalLafClassName = plaf;
  19. try {
  20. UIManager.setLookAndFeel(finalLafClassName);
  21. SwingUtilities.updateComponentTreeUI(frame);
  22. } catch (Exception exception) {
  23. System.out.print("Can't change look and feel.");
  24. }
  25.  
  26. }
  27. };
  28.  
  29. UIManager.LookAndFeelInfo UIlooks[] = UIManager.getInstalledLookAndFeels();
  30.  
  31.  
  32. for (int i = 0, n = UIlooks.length; i < n; i++) {
  33. JButton button = new JButton(UIlooks[i].getName());
  34. button.setActionCommand(UIlooks[i].getClassName());
  35. button.addActionListener(changeUI);
  36. panel.add(button);
  37. }
  38.  
  39. frame.getContentPane().setLayout(new GridLayout(1,1));
  40. frame.getContentPane().add(comboBox);
  41. frame.getContentPane().add(txtArea);
  42. frame.getContentPane().add(panel);
  43. frame.setSize(580, 250);
  44. frame.setVisible(true);
  45. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  46. }
  47. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Android Developer
Mobile: 09079373999
Telephone: 826-9296
E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment