JButton

This tutorial is now obsolete. Please see updated materials in the coreservlets.com Java programming tutorials.


1. JButton Basics

Simple uses of JButton are very similar to Button. You create a JButton with a String as a label, and then drop it in a window. Events are normally handled just as with a Button: you attach an ActionListener via the addActionListener method.

2. New Features: Icons, Alignment, and Mnemonics

The most obvious new feature is the ability to associate images with buttons. Swing introduced a utility class called ImageIcon that lets you very easily specify an image file (jpeg or GIF, including animated GIFs). Many Swing controls allow the inclusion of icons. The simplest way to associate an image with a JButton is to pass the ImageIcon to the constructor, either in place of the text or in addition to it. However, a JButton actually allows seven associated images:
  1. the main image (use setIcon to specify it if not supplied in the constructor),
  2. the image to use when the button is pressed (setPressedIcon),
  3. the image to use when the mouse is over it (setRolloverIcon, but you need to call setRolloverEnabled(true) first),
  4. the image to use when the button is selected and enabled (setSelectedIcon),
  5. the image to use when the button is disabled (setDisabledIcon),
  6. the image to use when it is selected but disabled (setDisabledSelectedIcon), and
  7. the image to use when the mouse is over it while it is selected (setRolloverSelectedIcon).

You can also change the alignment of the text or icon in the button (setHorizontalAlignment and setVerticalAlignment; only valid if button is larger than preferred size), and change where the text is relative to the icon (setHorizontalTextPosition, setVerticalTextPosition).

You can also easily set keyboard mnemonics via setMnemonic. This results in the specified character being underlined on the button, and also results in ALT-char activating the button.

3. New Feature in JDK 1.2.2 Only: HTML in Button Label

In JDK 1.2.2 and Swing 1.1.1 (and later), Sun added the ability to use HTML to describe the text in JButtons and JLabels. This lets you easily have multi-line labels, mixed fonts and colors, and other fancy features. See the Swing Tutorial section on JLabel for details.

4. JButton Example: Source Code

Here is an example of four similar buttons: one with plain text, one with just an image, one with both and the default layout (image to the left, text to the right), and one with the image and text positions reversed.

JButtons.java (Download source code)

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

public class JButtons extends JFrame {
  public static void main(String[] args) {
    new JButtons();
  }

  public JButtons() {
    super("Using JButton");
    WindowUtilities.setNativeLookAndFeel();
    addWindowListener(new ExitListener());
    Container content = getContentPane();
    content.setBackground(Color.white);
    content.setLayout(new FlowLayout());
    JButton button1 = new JButton("Java");
    content.add(button1);
    ImageIcon cup = new ImageIcon("images/cup.gif");
    JButton button2 = new JButton(cup);
    content.add(button2);
    JButton button3 = new JButton("Java", cup);
    content.add(button3);
    JButton button4 = new JButton("Java", cup);
    button4.setHorizontalTextPosition(SwingConstants.LEFT);
    content.add(button4);
    pack();
    setVisible(true);
  }
}
Note: also requires WindowUtilities.java and ExitListener.java, shown earlier, plus cup.gif.

4. JButton Example: Result

Using images with JButtons


This page is part of my Quick Swing Tutorial for AWT Programmers. © 1999 Marty Hall. All source code freely available for unrestricted use. Created for for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and Java short courses.