Open In App

Java AWT Button

Last Updated : 26 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

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

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:

GIF of Java AWT Button Example 1

Final Output Created:

Java AWT Button 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:

GIF for Java AWT Button Example-2

Final Output Created:

Output of Java AWT Button Example 2

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:

GIF of Java AWT Button Example-3

Final Output Created:

Output of  Java AWT Button Example-3



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads