Java Reference
In-Depth Information
JButton Class
The
JButton
component is the basic
AbstractButton
component that can be selected. It supports
text, images, and HTML-based labels, as shown in Figure 4-12.
Figure 4-12.
Sample JButton components
Creating a JButton
The
JButton
class has five constructors:
public JButton()
JButton button = new JButton();
public JButton(Icon image)
Icon icon = new ImageIcon("dog.jpg");
JButton button = new JButton(icon);
public JButton(String text)
JButton button = new JButton("Dog");
public JButton(String text, Icon icon)
Icon icon = new ImageIcon("dog.jpg");
JButton button = new JButton("Dog", icon);
public JButton(Action action)
Action action = ...;
JButton button = new JButton(action);
You can create a button with or without a text label or icon. The icon represents the default
or
selected
icon property from
AbstractButton
.
■
Note
Creating a
JButton
from an
Action
initializes the text label, icon, enabled status, and tooltip text.
In addition, the
ActionListener
of the
Action
will be notified upon button selection.