Change User Interface(UI) in Java

Submitted by: 
Language: 
Visitors have accessed this post 795 times.

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[] { "<a href="http://www.sourcecodester.com"" rel="nofollow">www.sourcecodester.com"</a>, "This is a List." });
  3. JTextArea txtArea = new JTextArea("<a href="http://www.facebook.com/BermzISware" rel="nofollow">www.facebook.com/BermzISware</a> \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[] { "<a href="http://www.sourcecodester.com"" rel="nofollow">www.sourcecodester.com"</a>, "This is a List." });
  10. JTextArea txtArea = new JTextArea("<a href="http://www.facebook.com/BermzISware" rel="nofollow">www.facebook.com/BermzISware</a> \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 Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.