Return to Course Content Home

JButtons

Optional Reading

We will be using the Creating a GUI with JFC/Swing tutorial from Sun as a Reference source. You are not required to read the entire tutorial, I suggest you skim through it though to use a a supplement to what I give you here.

AbstractButton

There are actually multiple "flavors" of buttons in Java. All of these varieties inherit from AbstractButton:

Button classes

When it comes down to it, all buttons are very simple, they have two states, and some stay pushed when pressed and others release after being pressed.

JButtons are like JLabels in that they can have text (yes HTML too!) and icons. You can control where the text and images are placed within the button if you wish.

With most buttons, you will just use an ActionListener to watch for a button press. Sometimes with JRadioButtons, you may need to use an ChangeListener because you may want to keep track of what button just lost the "pressed" state as well as the newly pressed button.

All of the flavors of buttons have multiple constructors, usually allowing for text, icon and if the button is initially selected or not for classes of the JToggleButton variety.

Believe it or not, the only difference between a JCheckBox and a JRadioButton is the little square or circle icon for the button! The functionality of only one JRadioButton being selected at a time is if it has been added to a ButtonGroup object. The ButtonGroup ensures only one button is selected at a time.

Buttons are created and then have action listeners attached to them.

There is something called an intent indicator that should be used with button. If the button causes and additional window to pop up, the text should be followed by an ellipsis (...). This lets the user know that they should expect an intermediate action to occur before the action series is completed. This applies to menus as well. Look at your menu system in any application. "Save" doesn't have ellipsis because it happens immediately, but "Save As..." does, because another window comes up asking for a file name.

You can have the Icon associated with a button change depending on how the user is interacting with the button. There are methods to set:

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

Download and open the Netbeans project buttons. When you run this project, you'll get the following window:

Buttons

In this example, I've tried to show the usage of regular buttons, check boxes, and radio buttons. Try clicking on the different buttons to see how they work. In particular, notice the icons I gave to the Duke JCheckBox button. When you provide an icon, it replaces the conventional check box. This means you always need to supply a selected icon as well. What happens when you click on this button?