O'Reilly logo

Java, A Beginner’s Guide, 5th Edition by Herbert Schildt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Use JButton

One of the most commonly used Swing controls is the push button. A push button is an instance of JButton. JButton inherits the abstract class AbstractButton, which defines the functionality common to all buttons. Swing push buttons can contain text, an image, or both, but this book uses only text-based buttons.

JButton supplies several constructors. The one used here is

JButton(String msg)

Here, msg specifies the string that will be displayed inside the button.

When a push button is pressed, it generates an ActionEvent. ActionEvent is defined by the AWT and also used by Swing. JButton provides the following methods, which are used to add or remove an action listener:

void addActionListener(ActionListener al)

void removeActionListener(ActionListener ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required