Java Button Click Event Example
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 using JFrame.
Contents
- 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 Handling Java Swing Button Click Event Step by Step
- 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
- 3 Which Method is Used to Handle Button Click Event?
- 4 Which Event is Performed on the Button Click in Java?
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 handleswindow 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 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
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.
//importing all necessary packages import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; |
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.
import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; public class MainClass { //creating main method public static void main ( String [ ] args ) { } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; class ActionEventDemo { JFrame frame = new JFrame ( ) ; //creating object of JFrame class ActionEventDemo ( ) { prepareGUI ( ) ; //calling prepareGUI() method } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; //Setting title of JFrame frame . getContentPane ( ) . setLayout ( null ) ; //Setting Layout frame . setVisible ( true ) ; //Setting visibility frame . setBounds ( 200 , 200 , 400 , 400 ) ; //Setting Location and Size frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; //Setting default close operation } } public class MainClass { public static void main ( String [ ] args ) { } } |
- Now create an object of the ActionEventDemo class inside the main method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; class ActionEventDemo { JFrame frame = new JFrame ( ) ; ActionEventDemo ( ) { prepareGUI ( ) ; } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; frame . getContentPane ( ) . setLayout ( null ) ; frame . setVisible ( true ) ; frame . setBounds ( 200 , 200 , 400 , 400 ) ; frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; } } public class MainClass { public static void main ( String [ ] args ) { new ActionEventDemo ( ) ; //Creating object of ActionEventDemo class } } |
- 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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; class ActionEventDemo { JFrame frame = new JFrame ( ) ; JButton button = new JButton ( "Click Me" ) ; //Creating object of JButton class ActionEventDemo ( ) { prepareGUI ( ) ; buttonProperties ( ) ; //Calling buttonProperties() method } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; frame . getContentPane ( ) . setLayout ( null ) ; frame . setVisible ( true ) ; frame . setBounds ( 200 , 200 , 400 , 400 ) ; frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; } public void buttonProperties ( ) { button . setBounds ( 130 , 200 , 100 , 40 ) ; //Setting location and size of button frame . add ( button ) ; //adding button to the frame } } public class MainClass { public static void main ( String [ ] args ) { new ActionEventDemo ( ) ; } } |
- 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
Step 5
- Here is an example of how we can implement ActionListener interface into any class.
class classname implements ActionListener |
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; //implementing ActionListener interface class ActionEventDemo implements ActionListener { JFrame frame = new JFrame ( ) ; JButton button = new JButton ( "Click Me" ) ; ActionEventDemo ( ) { prepareGUI ( ) ; buttonProperties ( ) ; } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; frame . getContentPane ( ) . setLayout ( null ) ; frame . setVisible ( true ) ; frame . setBounds ( 200 , 200 , 400 , 400 ) ; frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; } public void buttonProperties ( ) { button . setBounds ( 130 , 200 , 100 , 40 ) ; frame . add ( button ) ; } //Overriding actionPerformed() method @ Override public void actionPerformed ( ActionEvent e ) { } } public class MainClass { public static void main ( String [ ] args ) { new 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; class ActionEventDemo implements ActionListener { JFrame frame = new JFrame ( ) ; JButton button = new JButton ( "Click Me" ) ; ActionEventDemo ( ) { prepareGUI ( ) ; buttonProperties ( ) ; } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; frame . getContentPane ( ) . setLayout ( null ) ; frame . setVisible ( true ) ; frame . setBounds ( 200 , 200 , 400 , 400 ) ; frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; } public void buttonProperties ( ) { button . setBounds ( 130 , 200 , 100 , 40 ) ; frame . add ( button ) ; button . addActionListener ( this ) ; //Registering ActionListener to the button } @ Override public void actionPerformed ( ActionEvent e ) { } } public class MainClass { public static void main ( String [ ] args ) { new ActionEventDemo ( ) ; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import javax . swing . * ; import java . awt . * ; import java . awt . event . * ; class ActionEventDemo implements ActionListener { JFrame frame = new JFrame ( ) ; JButton button = new JButton ( "Click Me" ) ; ActionEventDemo ( ) { prepareGUI ( ) ; buttonProperties ( ) ; } public void prepareGUI ( ) { frame . setTitle ( "My Window" ) ; frame . getContentPane ( ) . setLayout ( null ) ; frame . setVisible ( true ) ; frame . setBounds ( 200 , 200 , 400 , 400 ) ; frame . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ; } public void buttonProperties ( ) { button . setBounds ( 130 , 200 , 100 , 40 ) ; frame . add ( button ) ; button . addActionListener ( this ) ; } @ Override public void actionPerformed ( ActionEvent e ) { //Changing Background Color frame . getContentPane ( ) . setBackground ( Color . pink ) ; } } public class MainClass { public static void main ( String [ ] args ) { new ActionEventDemo ( ) ; } } |
- 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
Source: https://www.tutorialsfield.com/jbutton-click-event/
0 Response to "Java Button Click Event Example"
Post a Comment