Home » Java AWT Button

Java AWT Button

A button is basically a control component with a label that generates an event when pushed. The Button class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed.

When we press a button and release it, AWT sends an instance of ActionEvent to that button by calling processEvent on the button. The processEvent method of the button receives the all the events, then it passes an action event by calling its own method processActionEvent. This method passes the action event on to action listeners that are interested in the action events generated by the button.

To perform an action on a button being pressed and released, the ActionListener interface needs to be implemented. The registered new listener can receive events from the button by calling addActionListener method of the button. The Java application can use the button’s action command as a messaging protocol.

AWT Button Class Declaration

Button Class Constructors

Following table shows the types of Button class constructors

Sr. no. Constructor Description
1. Button( ) It constructs a new button with an empty string i.e. it has no label.
2. Button (String text) It constructs a new button with given string as its label.

Button Class Methods

Sr. no. Method Description
1. void setText (String text) It sets the string message on the button
2. String getText() It fetches the String message on the button.
3. void setLabel (String label) It sets the label of button with the specified string.
4. String getLabel() It fetches the label of the button.
5. void addNotify() It creates the peer of the button.
6. AccessibleContext getAccessibleContext() It fetched the accessible context associated with the button.
7. void addActionListener(ActionListener l) It adds the specified action listener to get the action events from the button.
8. String getActionCommand() It returns the command name of the action event fired by the button.
9. ActionListener[ ] getActionListeners() It returns an array of all the action listeners registered on the button.
10. T[ ] getListeners(Class listenerType) It returns an array of all the objects currently registered as FooListeners upon this Button.
11. protected String paramString() It returns the string which represents the state of button.
12. protected void processActionEvent (ActionEvent e) It process the action events on the button by dispatching them to a registered ActionListener object.
13. protected void processEvent (AWTEvent e) It process the events on the button
14. void removeActionListener (ActionListener l) It removes the specified action listener so that it no longer receives action events from the button.
15. void setActionCommand(String command) It sets the command name for the action event given by the button.

Note: The Button class inherits methods from java.awt.Component and java.lang.Object classes.

Java AWT Button Example

Example 1:

ButtonExample.java

To compile the program using command prompt type the following commands

If there’s no error, we can execute the code using:

Output:

Java AWT Button

Example 2:

Output:

Java AWT Button

Java AWT Button Example with ActionListener

Example:

In the following example, we are handling the button click events by implementing ActionListener Interface.

ButtonExample3.java

Output:

Java AWT Button


Next TopicJava AWT Label

You may also like