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.
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
|
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
Examples of Java AWT Button
Let us understand the AWT Button class and their methods with some examples given below:
Example 1 (Basic Button):
The example given below is a simple implementation of a Button with a label.
Java
// Java AWT Program to create
// Basic Button
import java.awt.Button;
import java.awt.Frame;
// Driver Class
public class Main {
// main function
public static void main(String[] args)
{
// Create a frame
Frame frame = new Frame("AWT Button Example");
// Create a button
Button button = new Button("Click Me!");
// Set the button position on the frame
button.setBounds(150, 130, 100, 30);
// Add the button to the frame
frame.add(button);
// Set the frame size and layout
frame.setSize(400, 300);
frame.setLayout(null);
// Set the frame visibility to true
frame.setVisible(true);
}
}
Run the code using the following commands:
javac Main.java
java Main
Output:

Final Output Created:

Example 2 (Adding Multiple Properties):
In the below example, there are multiple buttons each having different properties and attributes.
Java
// Java AWT Program for
// Adding Multiple Properties
import java.awt.*;
import java.awt.Font;
// Driver Class
public class Main {
// main function
public static void main(String[] args){
// Create a frame
Frame frame = new Frame("AWT Button Example");
// Button 1
Button button1 = new Button("Click Me!");
// Set the background color
button1.setBackground(Color.BLUE);
// Set the foreground color
button1.setForeground(Color.WHITE);
// Set the button font size
button1.setFont(new Font("Arial", Font.BOLD, 14));
// Set the button position
button1.setBounds(150, 50, 100, 50);
// Button 2 with different properties
Button button2 = new Button("Press Me!");
button2.setBackground(Color.GREEN);
button2.setForeground(Color.BLACK);
button2.setFont(new Font("Times New Roman", Font.PLAIN, 16));
button2.setBounds(150, 140, 80, 30);
// Button 3 with different properties
Button button3 = new Button("Tap Me!");
button3.setBackground(Color.RED);
button3.setForeground(Color.WHITE);
button3.setFont(new Font("Verdana", Font.ITALIC, 16));
button3.setBounds(130, 220, 150, 80);
// Add the buttons to the frame
frame.add(button1);
frame.add(button2);
frame.add(button3);
// Set the frame background color
frame.setBackground(Color.LIGHT_GRAY);
// Set the frame size and make it visible
frame.setSize(400, 400);
frame.setLayout(null);
// Set the frame visibility to true
frame.setVisible(true);
}
}
Output:

Final Output Created:

Example 3 (ActionListener):
In this example, an ActionListener is called when the button is clicked. It triggers an event and the label is displayed on the screen.
Java
// Java AWT Program to demonstrate
// Button with ActionListener
import java.awt.*;
import java.awt.event.*;
// Driver Class
public class Main {
// main function
public static void main(String[] args)
{
// Create a frame
Frame frame = new Frame("AWT Button Example");
// Create a button
Button b = new Button("Click Here!");
// Set the position of the button
b.setBounds(160, 80, 80, 40);
// Add a background color
b.setBackground(Color.GREEN);
// Create a label
Label label = new Label();
// Set the position of the label
label.setBounds(80, 140, 280, 20);
// Set the properties of the label
label.setForeground(Color.BLUE);
label.setFont(new Font("Arial", Font.BOLD, 14));
// Add an action listener to the button
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
label.setText(
"Hey geek, Welcome to GeeksforGeeks!!");
}
});
// Add button to the frame
frame.add(b);
// Add Label to the frame
frame.add(label);
// Add background color to the frame
frame.setBackground(Color.LIGHT_GRAY);
// Set the size, layout and visibility
frame.setSize(400, 300);
frame.setLayout(null);
frame.setVisible(true);
}
}
Output:

Final Output Created:
Similar Reads
Java AWT Checkbox
Java AWT (Abstract Window Toolkit) provides a really various set of tools for edifice in writing user interfaces (GUIs), and among these tools is the Checkbox class. Checkboxes are necessary components for user interactions, allowing users to work binary choices easily. In this clause, we'll search
4 min read
Java AWT | FlowLayout
FlowLayout is used to arrange components in a sequence one after the other. The default layout of applet and panel is FlowLayout. Constructors : FlowLayout(): It will Construct a new FlowLayout with centered alignment.The horizontal and vertical gap will be 5 pixels. FlowLayout(int align) : It will
5 min read
Java ItemListener in AWT
The Java ItemListener user interface in Java's Abstract Window Toolkit (AWT) is a crucial component for managing user interactions with elements like checkboxes and option lists. In this article, we wish to search how the ItemListener interface is old in AWT to respond to exploiter input. The ItemLi
3 min read
Java AWT CheckboxGroup
Java AWT CheckboxGroup is a class that is used to create checkboxes and group a set of checkbox buttons together. Many components in the CheckboxGroup separate, which serves as a means to group a set of checkboxes. In this clause, we will delve into the CheckboxGroup class, and its methods, and demo
3 min read
Blaze UI Buttons
Blaze UI is a CSS open-source framework. It is a lightweight UI toolkit that provides great tools for building customized and scalable applications. It can work with any framework that exists. It can adapt to any ecosystem. All designs or CSS are mobile-first and hence responsive. Its project is ava
6 min read
Java AWT List
AWT or Abstract Window Toolkit is a set of various UI components with the List Component. We can display a scrollable list of items through which users can select one or multiple options. AWT List is embedded into the AWT library which is used to build a GUI to present a set of choices to the user.
9 min read
Introduction to Java
Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read
Java Features
Java is a high-level, object-oriented programming language. It is known for its platform independence, reliability, and security. Java Programming language follows the "Write Once, Run Anywhere" principle. It provides various features like portability, robustness, simplicity, multithreading, and hig
6 min read
Button in Android
In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it. This article demonstrates how to create a button in Android Studio.Class Hierarchy of the Button Class in Kotlinkotlin.
3 min read
Java - Close AWT Window
Java Abstract Window Toolkit (AWT) is a package that creates dynamic and interactive Graphical User Interfaces (GUIs) for Java applications. The ability to properly close AWT windows is an important aspect of developing user-friendly AWT applications. In this article, we will look into different met
7 min read