Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 generated if a button is clicked in AWT?
4.2.
What GET button method returns?
4.3.
How do event listeners work?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java AWT Button

Author Sanjana Yadav
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

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 Methods

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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:

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:

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:

Try it by yourself on java online compiler.

Frequently Asked Questions

What is generated if a button is clicked in AWT?

By invoking processEvent on a button when it is pushed and released, AWT delivers an instance of ActionEvent to it. The button's processEvent function gets all of the button's events and calls its own processActionEvent method to send an action event forward.

What GET button method returns?

getButton. Returns which of the mouse buttons has changed state, if any have. The value returned is in the range of 0 to MouseInfo. getNumberOfButtons() returns the number of buttons.

How do event listeners work?

Often, the object that creates the event registers an event listener. When an event happens, the object iterates across all registered listeners, telling them of the occurrence.

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 Coding Ninjas Studio 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 Coding Ninjas Studio! 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!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Previous article
Java AWT Basics
Next article
Java AWT Label
Live masterclass