Table of contents
1.
Introduction
2.
Button class
2.1.
AWT Button Class Declaration
2.2.
Button Class Constructors
2.3.
Button Class Methods
3.
Examples
3.1.
Java AWT Button Example with ActionListener
4.
Frequently Asked Questions
4.1.
What is a Java button?
4.2.
How to start a button in Java?
4.3.
How to detect a key press in Java?
5.
Conclusion
Last Updated: Nov 28, 2024
Easy

Java AWT Button

Author Sanjana Yadav
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A button is a control component with a label that, when pressed, causes an event. The Button class is used to construct a labeled button with platform independence. When the button is pressed, the application performs some action.

When we push and release a button, AWT sends an instance of ActionEvent to it by running processEvent on the button. The button's processEvent method accepts all events and then passes an action event by calling its own method processActionEvent. This technique forwards the action event to action listeners that are interested in the button's action events.

Java AWT Button

Let us understand the AWT button in detail, with the help of examples.

Button class

The Button class is used to construct a push-button control that can trigger an ActionEvent when clicked. The ActionListener interface must be implemented to handle a button click event. A button is a component that extends the JComponent class that may be applied to a container such as Frame or a component such as Panel.

AWT Button Class Declaration

public class Button extends Component implements Accessible  

Button Class Constructors

The table below lists the several types of Button class constructors.

Button Class Constructors

Button Class Methods

Button Class Methods
Button Class Methods

Examples

Let’s go through an example to have a better understanding of AWT Button in Java.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewClass {
    Frame jf;
    Button b1, b2, b3;

    NewClass() {
        jf = new JFrame("Frame buttons");
        b1 = new Button();
        b2 = new Button("Click me");
        b3 = new Button();


        b3.setLabel("Third Button");


        jf.add(b1);
        jf.add(b2);
        jf.add(b3);


        jf.setLayout(new FlowLayout());
        jf.setSize(400, 200);
        jf.setVisible(true);
    }
    public static void main(String... ar) {
        new NewClass();
    }
}

Output

When you execute the code, you'll see the following window:

output

You can also read about the Multiple Inheritance in Java.

Java AWT Button Example with ActionListener

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class NewClass implements ActionListener {
    Frame jf;
    Button b1, b2;
    Label label;

    NewClass() {
        jf = new Frame("Example Buttons");
        b1 = new Button("Yes");
        b2 = new Button("No");
        label = new Label();

        jf.add(b1);
        jf.add(b2);
        jf.add(label);


        b1.addActionListener(this);
        b2.addActionListener(this);
        
        jf.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 20));
        jf.setSize(400, 250);
        jf.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("Yes")) {
            label.setText("Welcome to Coding Ninjas!! You have pressed 'Yes' button");
            jf.add(label);
            jf.setVisible(true);
        }


        if (ae.getActionCommand().equals("No")) {
            label.setText("You have pressed the 'No' button");
            jf.add(label);
            jf.setVisible(true);
        }
    }


    public static void main(String... ar) {
        new NewClass();
    }
}

Output

When you execute the code, you'll see the following window:

output

When you click on any of the buttons, you will be presented with an appropriate message; for example, when we click on the Yes button, we are presented with the following message:

output

Try it by yourself on java online compiler.

Frequently Asked Questions

What is a Java button?

A Java button is a graphical component in Swing or AWT (e.g., JButton) that allows users to perform actions when clicked.

How to start a button in Java?

Create a JButton object, set its properties, and add it to a container. Attach an ActionListener to define its behavior on click.

How to detect a key press in Java?

Use a KeyListener to handle key events. Implement its keyPressed method to define actions when a specific key is pressed.

Conclusion

In this article, we have extensively discussed the Java AWT Button. We learned to create a button using java and AWT. 

We hope that this blog has helped you enhance your knowledge of Java and java awt and if you would like to learn more about java, check out our articles, java archivesDifference Between AWT And Swing In Java | Coding Ninjas Blog

Refer to our Guided Path on Code360 to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Code360! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Live masterclass