There are several ways to handle events on buttons. The following tutorial shows you how to handle events on buttons in Java Swing.
Detect button events using an event listener
To detect button events, you need to use an event listener. An event listener is a class that is registered with the object that will fire the events.
In this case, the object is the button. When the button is clicked, the event listener is called and can take appropriate action.
Simple Java Swing Program for Button Event Handling
The listener class must implement the ActionListener interface, which has a single method, actionPerformed(). This method is called when the event occurs.
Simple Java program for beginners. You can practice these programs yourself without copying and pasting. Just learn how to write your own code. Take help from the below program only.
import javax.swing.*; public class FirstSwingExample extends JFrame{ FirstSwingExample() { JFrame frm=new JFrame("First Swing Example");//creating instance of JFrame JButton btn = new JButton("Click Me");//creating instance of JButton btn.setBounds(130, 100, 100, 40);//x axis, y axis, width, height frm.add(btn);//adding button in JFrame frm.setSize(400, 500); //400 width and 500 height frm.setLayout(null);//using no layout managers frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frm.setVisible(true);//making the frame visible } public static void main(String[] args) { new FirstSwingExample(); } }
 Handle different types of button events including mouse click
In this tutorial, we will show you how to handle different types of button events, including a mouse click. We will create a simple Java Swing program with one button.
When the user clicks the button, we will print a message to the console. First, we will create the button and specify its properties. Next, we will create an event listener for the button. Lastly, we will write the code to handle the button event.
Handle mouse motion events for tracking button movements
In order to track the mouse motion, we need to add an event listener to the button. This time, we’ll use the MouseMotionAdapter class.
The MouseMotionAdapter class will allow us to track the mouse’s x and y coordinates whenever it moves. Let’s add the code to track the mouse motion to our previous example:
The program can be started on a button click
To get started, we’ll create a class that will be responsible for handling our events. This is a common practice in Java programming and will help keep our code organized and easy to follow.
We’ll create a new class called “EventHandler” and make it a subclass of JFrame. This will allow us to create a window that will act as our main interface. In the constructor, we’ll set up a listener for our “button clicked” event.
This is the event that will be fired when the user clicks on our button. We’ll also create a method called “handleEvent” that will take care of all the event handling for our button.
Conclusion:
Handling button events is a very common task in Java. The various types of button event listeners are JButton, JMenuItem and all components derived from AbstractButton class.
The program can be started onclick of the specific buttons by using an anonymous class to implement the ActionListener interface.