Button in Java AWT

The ‘Button’ class is a part of the ‘java.awt’ package which has a collection of classes and methods for creating GUI components. Java AWT Buttons can be used to perform several actions like saving a file, closing a window, submitting a form, or triggering any specific action.

When we press a button, AWT creates an instance of ActionEvent and delivers it by calling processEvent on the button. The processEvent method of the button accepts all events and then sends an action event to its own function processActionEvent. The ActionListener interface must be implemented if one wants to execute an action when a button is pressed.

Syntax of Java AWT Button

public class Button extends Component implements Accessible  

Constructors of Button

There are 2 types of Constructors in the AWT Button class.

  • Button(): Creates a new button with the label as an empty string.
  • Button(String label): Creates a new button with the label as the specified string as a parameter.

Java AWT Button Class methods

List of all the methods associated with the AWT button and its Description

Method

Description

void addActionListener(ActionListener l)

The action listener is added to receive action events from this button.

void addNotify()

Creates the peer of the button.

AccessibleContext getAccessibleContext()

The method returns the AccessibleContext associated with this Button.

String getActionCommand()

The method returns the command name of the action event triggered by this button.

ActionListener[] getActionListeners()

The method returns an array of all the action listeners that have been registered on this button.

String getLabel()

Retrieves the label of this button.

<T extends EventListener> T[] getListeners(Class<T> listenerType)

The method returns an array of all the objects that are currently registered as FooListeners on this Button.

protected String paramString()

Returns a string that represents the status of this Button.

protected void processActionEvent(ActionEvent e)

Sends action events to any registered ActionListener objects in order to process them when they occur on this button.

protected void processEvent(AWTEvent e)

Processes events on this button.

void removeActionListener(ActionListener l)

The specified action listener is removed from the button.

void setActionCommand(String command)

Configures the action event by setting the command name.

void setLabel(String label)

Sets the button’s label to be the specified string.

Inherited Methods

The Methods included with AWT button are inherited by:

  • java.awt.Component
  • java.lang.Object

Java AWT Button

Abstract Window Toolkit, also known as AWT is a library for creating Graphical User Interfaces (GUIs) in Java applications. Within the Java AWT library, you’ll find a collection of classes and methods designed to develop windows and essential user interface elements. The Button class is a control component in AWT, that triggers an event when it is clicked.

Similar Reads

Button in Java AWT

The ‘Button’ class is a part of the ‘java.awt’ package which has a collection of classes and methods for creating GUI components. Java AWT Buttons can be used to perform several actions like saving a file, closing a window, submitting a form, or triggering any specific action....

Examples of Java AWT Button

Let us understand the AWT Button class and their methods with some examples given below:...