Java Button Click Event Tutorial For Beginners-JButton Listener

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, and if you haven’t seen my previous tutorial yet, then you can click on the link given below.

Javax Swing JButton

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.

Prefer video? Watch this

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

  • It is an object which is notified when an event occurs.
  • Listeners are Java interfaces and since we are covering advance topics you must have the knowledge of interfaces that when we implement an interface by a class we must implement all interface’s methods because all the methods in an interface are abstract . Implementing an interface without adding its required abstract methods result in a syntax error .
  • A listener has two major requirements,
    • It must have been registered with one or more sources to receive notifications about specific types of events.
    • It must implement methods to receive and process these notifications.
  • Some common Java interfaces and the events they listen for and handle :
Java Interfaces Event They Listen
ActionListener Listens for and handles button clicks
KeyListener Listens for and handles key events
MouseListener Listens for and handles mouse events
MouseMotionListener Listens for and handles mouse drag and move events
TextListener Listens for and handles text changing events
ItemListener Listens for and handles CheckBox and Listbox events
AdjustmentListener Listens for and handles scrolling events
Runnable Listens for and handles Threads and Run
WindowListener Listens for and handles  window events
FocusListener Listens for and handles focus events

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 :
Events Classes Description
ActionEvent Generated when an action has occured (e.g- button pressed)
AdjustmentEvent Generated when scroll bar is manipulated
ContainerEvent Generated when container is changed ( added or removed )
FocusEvent Generated when component’s keyboard focus is changed
InputEvent Abstract superclass of KeyEvent and MouseEvent
ItemEvent Generated when an item is selected or deselected
KeyEvent Generated when input  is given through keyboard
MouseEvent Associated with the mouse events
TextEvent Generated when value of text area or text field is changed
WindowEvent Associated with the windows related events

Event Classes and Associated Listener Interfaces

Event Classes Associated Listener Interface
ActionEvent ActionListener
AdjustmentEvent AdjustmentListner
ContainerEvent ContainerListener
FocusEvent FocusListener
ComponentEvent ComponentListener
ItemEvent  ItemListener
KeyEvent KeyListener
MouseEvent MouseListener
TextEvent TextListener
WindowEvent WindowListener

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

Handling Java Swing Button Click Event Step by Step

Importing Packages

Step 1

  • 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

Step 2

  • 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

Step 3

  • 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.
  • Now create an object of the ActionEventDemo class inside the main method.
  • Now save your program, compile it and run it.
Java Button Click Event
Java Button Click Event – fig-1
  • 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

Step 4

  • 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.
  • Now save your program, compile it and run it.
Java Button Click Event
Java Button Click Event – fig-2
  • 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

Step 5

  • Here is an example of how we can implement ActionListener interface into any class.
  • 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

Step 6

  • 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

Step 7

  • 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.
  • Now save the program, compile it and run it.
Java Button Click Event
Java Button Click Event – fig-3
  • 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…..

4 thoughts on “Java Button Click Event Tutorial For Beginners-JButton Listener”

  1. Thank you for this, I’m teaching a teenager Java and this is a fantastic tutorial. I even understood it, which is helpful if I want to teach it. 😉

    Reply

Leave a Comment