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):

2 Likes