JButton Class

Package: javax.swing

The JButton class represents a button: a user interface component that the user can click to initiate an action.

Constructors

Constructor

Description

JButton()

Creates a new button with no initial text

JButton(String text)

Creates a new button with the specified text

Methods

Method

Description

void addActionListener(ActionListener l)

Registers an action event listener for the button. The parameter must be an instance of a class that implements the ActionListener interface.

doClick()

Triggers an action event for the button as though the user clicked it.

String getText()

Returns the text displayed by the button.

void setBorderPainted (boolean value)

Shows or hides the button’s border. The default setting is true (the border is shown).

void setContentArea Filled(boolean value)

Specifies whether the button’s background should be filled or left empty. The default setting is true (the background is filled in).

void setEnabled (boolean value)

Enables or disables the button. The default setting is true (enabled).

void setRollover Enabled(boolean value)

Enables or disables the rollover effect, which causes the border to get thicker when the mouse moves over the button. The default setting is true (rollover effect enabled).

void setText(String text)

Sets the text displayed by the button.

void setToolTipText (String text)

Sets the tooltip text that’s displayed if ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.