Java swing if button pressed

Java Button Click Event Tutorial For Beginners-JButton ActionListener

Hello Friends, In this tutorial, you will learn about Java Button Click Event in Java Swing. In my previous tutorial, you have seen how to create the Java Button in Swing, and if you haven’t seen my previous tutorial yet, then you can click on the link given below.

So you have learned how to create a Button in Java, But You don’t know How to perform JButton on click action on button in Java Swing which means if you click on the button, nothing happens because you have not added any action event associated with the JButton. So now in this tutorial, you are going to learn about Java Button Click Event or we can say Java button pressed Event using the JFrame step by step.

So Let’s start our Tutorial Java Button Click Event in Java Swing or JButton Click Event using JFrame.

Watch Video Tutorial

  • 1 Java Button Click Event
    • 1.1 What is An Event ??
    • 1.2 Event Source
    • 1.3 Event Listener
    • 1.4 Event Classes
    • 1.5 Event Classes and Associated Listener Interfaces
    • 2.1 Importing Packages
    • 2.2 Creating a class MainClass.java
    • 2.3 Creating another class ActionEventDemo.java
    • 2.4 Adding Java Button to JFrame
    • 2.5 Implementing ActionListener Interface
    • 2.6 Registering ActionListener to the JButton
    • 2.7 Performing Action Event

    Java Button Click Event

    What is An Event ??

    • It is an object which describes a change in a source.
    • Basically they are associated with a component.
    • Some examples of the events are Java Button Pressed Event, entering a character via keyboard, selecting an item in a list , clicking the mouse etc.
    • Events are supported by number of packages including java.util, java.awt, java.awt.event .

    Event Source

    • A source is an object that generates an Event .
    • This happens when the internal state of the object changes in some way.
    • A source can generate one or more types of events.
    • A method getSource() is associated with all events which returns the object on which the event is initially occurred.

    Event Listener

    Event Classes

    • Classes that represent events.
    • EventObject is the superclass for all events which means it is the root of the Java event class hierarchy.
    • EventObject contains two methods. getSource() and toString().
    • getSource() method returns the source and toString() method returns the string equivalent of the event .
    • AWTEvent is a subclass of EventObject class and it is superclass for all AWT events .
    • Package java.awt.event is used for defining interfaces and classed that is used for event handling in AWT and SWING.
    • Some common event classes in the package java.awt.event are given below :

    Event Classes and Associated Listener Interfaces

    So this was the brief description of event classes and listeners, and now we will see Java Button Click Event or Swing JButton click action using JFrame step by step in which we will learn about the JButton ActionListener interface, ActionPerformed() method and addActionListener() method.

    Handling Java Swing Button Click Event Step by Step

    Importing Packages

    • In the first step, we need to import all essential packages. In this program, we need to import another new package java.awt.event because we are dealing with event handling and this package provides classes and interfaces that are used for event handling in awt and Swing.

    Creating a class MainClass.java

    • In this step, create a class (MainClass.java in this example), and inside that class, there will be our main method.

    Creating another class ActionEventDemo.java

    • In this step, create another separate class (ActionEventDemo.java in this example).
    • Now create an object of the JFrame class.
    • Create a user-defined method prepareGUI(), and inside that method we will set the properties of the JFrame class like its title, its location and size, its default close operation, its visibility etc.
    • We will also change the Layout Manager of the frame’s content pane to null. By default frame’s content pane uses BorderLayout Manager as its Layout Manager.
    • Now we will create the constructor of the class ActionEventDemo and inside that constructor call the prepareGUI() method.

    Java Button Click Event

    • Now save your program, compile it and run it.
    • As you can see that we have successfully created our JFrame.
    • In next step, we will add a Java button to our JFrame.

    Adding Java Button to JFrame

    • Create an object of the JButton class.
    • Now again create another user-defined method buttonProperties() and inside that method set the location and size of the JButton using setBounds() method and finally add the JButton to the JFrame using add() method.

    Java Button Click Event

    • Now save your program, compile it and run it.
    • As you can see that we have successfully created and added the JButton to the JFrame.
    • Now we want that when we click on the button, some activity should be performed.
    • For this, first of all, we need to implement the JButton Listener which means ActionListener interface into our class so that we will be able to do some JButton click event..

    Implementing ActionListener Interface

    • If we have implemented the ActionListener interface in any class, then we must have to override its method which is actionPerformed(ActionEvent e) which takes a parameter ActionEvent (a class defined in package java.awt.event ). Now when someone clicks on the button the actionPerformed() method is called.
    • Now let’s implement the ActionListener interface into our class ActionEventDemo.

    Registering ActionListener to the JButton

    • In this step, we will add or can say register ActionListener to the JButton.
    • For this, we have to call addActionListner() method using the object of the JButton class. The parameter of the addActionListener() method is the object of that class in which ActionListener interface is implemented or can say in which we have defined actionPerformed() method. So if we are in the same class in which ActionListener interface is implemented, then we will pass this as an argument.

    Performing Action Event

    • Now we want that if we click on the button the background color of the frame’s content pane should be changed. For this, we will write the desired codes inside the actionPerformed() method. Which means the behaviour we want in response to the action is coded inside the actionPerformed() method.

    Java Button Click Event

    • Now save the program, compile it and run it.
    • Now we can see that as soon as we click on the button, the background color of the JFrame has been changed to pink.

    Which Method is Used to Handle Button Click Event?

    The Answer is “addActionListener()” method.

    addActionListener() method registers the ActionListener to the Java Button and the behaviour we want in response to the action is coded inside the actionPerformed() method. The actionPerformed() method is called just after the user executes any action.

    Which Event is Performed on the Button Click in Java?

    Then Answer is “Action Event”.

    The associated Listener interface is the ActionListener interface. When the user clicks on the button then the button fires an Action Event and that results in invocation of the ActionListener’s acitonPerformed() method.

    So, guys, I am wrapping up this tutorial “Java Button Click Event” in Java Swing and Feel free to drop us a comment if you find out something difficult to understand. After learning this, you can also learn how to create a Login Form in Java Swing .

    People are also Reading…..

    • Best Java Books For Beginners
    • Menu Driven Program in Java Using Switch Case
    • How to Create Calculator in Java Swing
    • How to Create Tic Tac Toe Game in Java
    • How to Create Login Form in Java Swing
    • Registration Form In Java with Database Connectivity
    • How to Create Splash Screen In Java
    • How to Create Mp3 Player in Java
    • How to Connect MySQL Database in Java Using NetBeans
    • 11 Best Site to Learn Java Online for Free

    Источник

    How to Check if a Button is Clicked in a Java Swing Application

    In this article, we show how we can check to see if a button is clicked in a Java Swing application.

    To be able to check if a button is clicked using Java, we create a button and add an event handler (or event listener) to the button, so that when the button is clicked, a method can be called. We can create the method to do anything such as output that the button was clicked or anything we want it to do.

    Alright, so now, let’s go through the code.

    Since this is a Swing application, we have to import the javax.swing package. We also have to import the java.awt.event package because this contains the ActionListener interface.

    So we create our JFrame class named Clickprogram. It extends the JFrame class because we’re creating a frame.

    We then create our main method. Since this is a singularly program, it needs a main method in order for the code to run.

    All we do in this main method is create an instance of the Clickprogram class so that we have a frame.

    We then create the constructor of the Clickprogram class. This constructor is so that we can initialize all the values of the frame that we create. Since the class we create is a frame class (it extends JFrame), we now add in what we want in the frame.

    So the first thing we do in the constructor is set the size of the frame. In this case, we set the frame to a width of 300px and a height of 200px.

    In almost any frame you build in Java, you want to add in the line, setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);. This is because when a window is created, it doesn’t terminate upon pressing the X button at the top right of the window. It simply vanishes but it doesn’t truly terminate operations. So in order to completely the end the GUI frame program, you have to specify this line. This completely ends the running of the program once the user Xs out.

    We then set the title of the frame to «Click Event».

    What we do next is we create an instance of the Clicklistener class. This is the class that you see later in the code that implements the ActionListener interface. For now, just realize we create an instance of this class and later when we create our button, we put this instance as a parameter to the addActionListener method implemented on the button. This ties an event handler to the button so that it calls this instance of the Clicklistener class.

    We then create a panel. Usually when you add components such as buttons, text fields, etc to a frame, you first add to a panel. And then you add the panel to the frame. So we create a panel named panel1.

    We then create a button named button1 that contains the phrase, «Click here».

    To this button, we add the addActionListener and inside of this method, we put click. This click represents an instance of the Clicklistener class. We’ll get to this below.

    We then add to the panel, button1.

    We then add to this frame, panel1. Similarly, you could leave off this and it would still work.

    We then make the frame visible.

    The next block of code is now the Clicklistener class which implements the ActionListener interface.

    We create a method actionPerformed, which in its parameter contains the ActionEvent class.

    If the source of the action created is equal to button1, then we change the text of the button to, «The button has been clicked».

    And this is all that is required to check if a button is clicked using Java.

    Источник

    Читайте также:  Неразрывный пробел php вставить
Оцените статью