Swing LookAndFeel Demo

The following source code demonstrates changing the look and feel of your operating system using Swing components:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.io.*;

import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.plaf.metal.MetalLookAndFeel;

JFrame frame;
JLabel label;
JTextArea txtArea;
JTextArea infoArea;

final int _wndW = 600;
final int _wndH = 450;

void openAction() {
  JFileChooser fileChooser = new JFileChooser();
  int i = fileChooser.showOpenDialog(fileChooser);
  if (i == JFileChooser.APPROVE_OPTION) {
    File file = fileChooser.getSelectedFile();
    String filepath = file.getPath();
    try {
      BufferedReader buffer = new BufferedReader(new FileReader(filepath));
      String s1 = "", s2 = "";
      while ((s1 = buffer.readLine())!= null) {
        s2 += s1 + "\n";
      }
      txtArea.setText(s2);
      buffer.close();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

void infoArea(int l, int t, int w, int h) {
  infoArea = new JTextArea();
  JScrollPane scrlPane = new JScrollPane(infoArea);
  scrlPane.setBounds(l, t, w, h);
  frame.add(scrlPane);
  infoArea.setEditable(true);
  infoArea.setFont(new Font("Menlo", Font.PLAIN, 12));
  infoArea.setLineWrap(false);
  infoArea.setWrapStyleWord(true);
  infoArea.repaint();
}

void displayLabel(int l, int t, int w, int h) {
  label = new JLabel();
  label.setHorizontalAlignment(JLabel.LEFT);
  label.setBounds(l, t, w, h);
  label.setText("Label for output display.");
  label.setToolTipText("label");
  frame.add(label);
  label.repaint();
}

void button(int l, int t, int w, int h) {
  JButton btn = new JButton("Open...");
  btn.setBounds(l, t, w, h);
  frame.add(btn);
  btn.addActionListener( e -> {
    openAction();
  }
  );
  btn.repaint();
}

void comboBox(int l, int t, int w, int h) {
  String country[]={"France", "Australia", "U.S.A", "England", "New Zealand"};
  JComboBox comboBox = new JComboBox(country);
  comboBox.setBounds(l, t, w, h);
  frame.add(comboBox);
  comboBox.repaint();
}

void listControl(int l, int t, int w, int h) {
  DefaultListModel<String> listArray = new DefaultListModel<>();
  listArray.addElement("Lamborghini");
  listArray.addElement("Lotus");
  listArray.addElement("Mercedes Benz");
  listArray.addElement("Rolls Royce");
  JList<String> list = new JList<>(listArray);
  list.setBounds(l, t, w, h);
  frame.add(list);
  list.addListSelectionListener( new ListSelectionListener() {
    void valueChanged(ListSelectionEvent evnt) {
      label.setText(list.getSelectedValue() + " was selected.");
    }
  }
  );
  list.repaint();
}

void textArea(int l, int t, int w, int h) {
  txtArea = new JTextArea();
  JScrollPane scrlPane = new JScrollPane(txtArea);
  scrlPane.setBounds(l, t, w, h);
  frame.add(scrlPane);
  txtArea.setEditable(true);
  txtArea.setFont(new Font("Menlo", Font.PLAIN, 14));
  txtArea.setLineWrap(false);
  txtArea.setWrapStyleWord(true);
  txtArea.repaint();
}

void buildWnd() {
  infoArea(20, 10, _wndW - 40, 100);
  displayLabel(310, 140, 200, 24);
  button(30, 140, 100, 24);
  comboBox(430, 170, 130, 24);
  listControl(310, 170, 100, 75);
  textArea(30, 170, 250, 200);
  frame.setVisible(true);
}

void setup() {
  surface.setVisible(false);
  frame = new JFrame();
  frame.setTitle("Swing Look and Feel Demo");
  frame.setBounds(100, 100, _wndW, _wndH);
  frame.setLayout(null);
  javax.swing.SwingUtilities.invokeLater(() -> {
    buildWnd(); // Builds components on EventDispatchThread
    try {
      for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
        infoArea.append(info.toString()+"\n");
      }
      UIManager.setLookAndFeel(new NimbusLookAndFeel());
      // UIManager.setLookAndFeel(new MetalLookAndFeel());
      SwingUtilities.updateComponentTreeUI(frame);
    }
    catch (UnsupportedLookAndFeelException e) {
      e.printStackTrace();
    }
  }
  );
}

Output for Nimbus (MacOS):

3 Likes

I like that!

When I tried it on my Chromebook, the title at the top doesn’t show. I’ve seen that with exported sketches too, so something peculiar with my goofy environment.

I have used swing buttons and sliders in my sketches in a separate window and been warned that it could cause random problems. This seems like a different way. I’ll play with it and see what I can do. I’d be interested in adding buttons or other components and have them do something in a running program.

As long as you build your swing components on a separate EventDispatchThread as shown in the demo I would not anticipate problems.

I would also expect that you should be able to create Swing components just like you usually do but be able to change their appearance based on the setting for the look and feel.

1 Like