Setting the look and feel
- Java has the capability to emulate many Operating Systems's look
and feel
- This includes
- Windows
- X-Window/Motif
- Macintosh (only available on macintosh machines)
- and the default Java look and feel
- The UIManager object is used to set the look and feel
- This can be done at the start of the program (before
initcomponents() has been run), or dynamically in reaction to a button
press or menu item choce.
- Code to set it at the begining of the program (within the
constructor) looks like the below. Note, the call to
UIManager.getSystemLookandFeelClassName will return a Windows look and
feel if you are running on Windows, a Macintosh look and feel for Macs,
and a Motif look and feel for Unix:
public class LookAndFeel extends javax.swing.JFrame {
/** Creates new form LookAndFeel */
public LookAndFeel() {
try{
String laf =
UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf); // tell the UIManager to change
the look and feel
System.out.println("Look and feel changed to "+laf);
}
catch (Exception e)
{
System.out.println("Unable to change
look and feel");
}
initComponents();
setTitle("Look and Feel
Demo");
setSize(600,400);
}
- To set the code dynamically, you do the same thing, except you
need to tell the frame to update its components (so they have the right
look and feel). The example below forces the Windows look and feel to
be used, regardless of the host system.
try{
String laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this); // tells swing to update
components
System.out.println("Look and feel changed to "+laf);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unable to set look and
feel","Error",JOptionPane.ERROR_MESSAGE);
}
- The following look and feel settings can be used:
- To make your program look like whatever the host system is :
- String
laf
= UIManager.getSystemLookAndFeelClassName();
-
UIManager.setLookAndFeel(laf); // tell the UIManager to change
the look and feel
- To make your program look like a Windows program (regardless of
you
host system)
- String laf =
"com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
-
UIManager.setLookAndFeel(laf);
- To make your program use the X-Windows/Motif (Unix) look and
feel
- String
laf
= "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
-
UIManager.setLookAndFeel(laf);
- To make your program use the default Java/Metal look and feel:
- String
laf
= UIManager.getCrossPlatformLookAndFeelClassName();
-
UIManager.setLookAndFeel(laf);
- The following application has the ability to click on the radio
buttons and dynamically change the look and feel. The other controls
don't do anything (except the Exit button), but are there so you can
see the difference.
- Click here to download
import javax.swing.*;
/*
* LookAndFeel.java
*
* Created on October 29, 2003, 10:08 AM
*/
/**
*
* @author mpenderg
*/
public class LookAndFeel extends javax.swing.JFrame {
/** Creates new form LookAndFeel */
public LookAndFeel() {
try{
String laf =
UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
System.out.println("Look and feel changed to "+laf);
}
catch (Exception e)
{
System.out.println("Unable to change look and feel");
}
initComponents();
setTitle("Look and Feel
Demo");
setSize(600,400);
}
/** This method is called from within the
constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The
content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
lookAndFeelGroup = new
javax.swing.ButtonGroup();
centerPanel = new
javax.swing.JPanel();
jPanel1 = new
javax.swing.JPanel();
defaultRadio = new
javax.swing.JRadioButton();
xwindowsRadio = new
javax.swing.JRadioButton();
hostRadio = new
javax.swing.JRadioButton();
windowsRadio = new
javax.swing.JRadioButton();
jTextField1 = new
javax.swing.JTextField();
jLabel1 = new
javax.swing.JLabel();
jScrollPane1 = new
javax.swing.JScrollPane();
jTextArea1 = new
javax.swing.JTextArea();
jComboBox1 = new
javax.swing.JComboBox();
toolBar = new
javax.swing.JToolBar();
exitButton = new
javax.swing.JButton();
menuBar = new
javax.swing.JMenuBar();
fileMenu = new
javax.swing.JMenu();
exitItem = new
javax.swing.JMenuItem();
editMenu = new
javax.swing.JMenu();
addWindowListener(new
java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
centerPanel.setLayout(null);
centerPanel.setMinimumSize(new java.awt.Dimension(0, 0));
jPanel1.setLayout(new
java.awt.GridLayout(2, 2));
jPanel1.setBorder(new
javax.swing.border.TitledBorder(new javax.swing.border.EtchedBorder(),
"Look and Feel"));
jPanel1.setPreferredSize(new
java.awt.Dimension(400, 400));
defaultRadio.setText("Default (Java)");
lookAndFeelGroup.add(defaultRadio);
defaultRadio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
defaultRadioItemStateChanged(evt);
}
});
jPanel1.add(defaultRadio);
xwindowsRadio.setText("Unix
Motif");
lookAndFeelGroup.add(xwindowsRadio);
xwindowsRadio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
xwindowsRadioItemStateChanged(evt);
}
});
jPanel1.add(xwindowsRadio);
hostRadio.setSelected(true);
hostRadio.setText("Host
System");
lookAndFeelGroup.add(hostRadio);
hostRadio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
hostRadioItemStateChanged(evt);
}
});
jPanel1.add(hostRadio);
windowsRadio.setText("Windows");
lookAndFeelGroup.add(windowsRadio);
windowsRadio.addItemListener(new java.awt.event.ItemListener() {
public void itemStateChanged(java.awt.event.ItemEvent evt) {
windowsRadioItemStateChanged(evt);
}
});
jPanel1.add(windowsRadio);
centerPanel.add(jPanel1);
jPanel1.setBounds(10, 20,
240, 90);
jTextField1.setColumns(20);
jTextField1.setToolTipText("null");
centerPanel.add(jTextField1);
jTextField1.setBounds(80,
120, 140, 20);
jLabel1.setText("Name :");
jLabel1.setToolTipText("null");
centerPanel.add(jLabel1);
jLabel1.setBounds(20, 120,
39, 20);
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jScrollPane1.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setViewportView(jTextArea1);
centerPanel.add(jScrollPane1);
jScrollPane1.setBounds(20,
160, 330, 80);
jComboBox1.setMaximumRowCount(5);
jComboBox1.setModel(new
javax.swing.DefaultComboBoxModel(new String[] { "Sunday", "Monday",
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }));
jComboBox1.setToolTipText("null");
centerPanel.add(jComboBox1);
jComboBox1.setBounds(270,
30, 100, 25);
getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);
exitButton.setText("Exit");
exitButton.setToolTipText("Exit Program");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
toolBar.add(exitButton);
getContentPane().add(toolBar, java.awt.BorderLayout.NORTH);
fileMenu.setMnemonic('f');
fileMenu.setText("File");
exitItem.setMnemonic('x');
exitItem.setText("Exit");
exitItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitItemActionPerformed(evt);
}
});
fileMenu.add(exitItem);
menuBar.add(fileMenu);
editMenu.setMnemonic('e');
editMenu.setText("Edit");
menuBar.add(editMenu);
setJMenuBar(menuBar);
pack();
}
private void
exitItemActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code
here:
System.exit(0);
}
private void
exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code
here:
System.exit(0);
}
private void
xwindowsRadioItemStateChanged(java.awt.event.ItemEvent evt) {
// Add your handling code
here:
if(!xwindowsRadio.isSelected())
return;
try{
String laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this); // tells swing to update
components
System.out.println("Look and feel changed to "+laf);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unable to set look and
feel","Error",JOptionPane.ERROR_MESSAGE);
}
}
private void
windowsRadioItemStateChanged(java.awt.event.ItemEvent evt) {
// Add your handling code
here:
if(!windowsRadio.isSelected())
return;
try{
String laf = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this); // tells swing to update
components
System.out.println("Look and feel changed to "+laf);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unable to set look and
feel","Error",JOptionPane.ERROR_MESSAGE);
}
}
private void
defaultRadioItemStateChanged(java.awt.event.ItemEvent evt) {
// Add your handling code
here:
if(!defaultRadio.isSelected())
return;
try{
String laf = UIManager.getCrossPlatformLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this); // tells swing to update
components
System.out.println("look and
feel changed to "+laf);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unable to set look and
feel","Error",JOptionPane.ERROR_MESSAGE);
}
}
private void
hostRadioItemStateChanged(java.awt.event.ItemEvent evt) {
// Add your handling code
here:
if(!hostRadio.isSelected())
return;
try{
String laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf);
SwingUtilities.updateComponentTreeUI(this); // tells swing to update
components
System.out.println("look and feel changed to "+laf);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this,"Unable to set look and
feel","Error",JOptionPane.ERROR_MESSAGE);
}
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent
evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new LookAndFeel().show();
}
// Variables declaration - do not modify
private javax.swing.JMenu fileMenu;
private javax.swing.JRadioButton defaultRadio;
private javax.swing.JRadioButton windowsRadio;
private javax.swing.JPanel jPanel1;
private javax.swing.JMenuBar menuBar;
private javax.swing.JRadioButton hostRadio;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton exitButton;
private javax.swing.JRadioButton xwindowsRadio;
private javax.swing.JPanel centerPanel;
private javax.swing.JComboBox jComboBox1;
private javax.swing.ButtonGroup lookAndFeelGroup;
private javax.swing.JToolBar toolBar;
private javax.swing.JMenuItem exitItem;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JMenu editMenu;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}