JButton

Handle action events for JButton

This is a tutorial on how to handle action events for JButtons components in a Java Desktop Application. JButtons are very frequently used, and it is a very standard practice to use buttons for basic input operations in GUI applications.

In order to handle action events in JButton, all you have to do is:

  • Create a class that extends JFrame and implements ActionListener.
  • Create new JButtons.
  • Override actionPerformed method of ActionListener interface.
  • Use ActionEvent.getActionCommand to get which JButton was pressed.
  • Use add to add JButtons to the frame.

Let’s see the following code snippet:

package com.javacodegeeks.snippets.desktop;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class HandleActionEventsForJButton extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	public HandleActionEventsForJButton() {

		// set flow layout for the frame
		this.getContentPane().setLayout(new FlowLayout());

		JButton button1 = new JButton("Yes");
		JButton button2 = new JButton("No");

		//set action listeners for buttons
		button1.addActionListener(this);
		button2.addActionListener(this);

		//add buttons to the frame
		add(button1);
		add(button2);

	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		String action = ae.getActionCommand();
		if (action.equals("Yes")) {
			System.out.println("Yes Button pressed!");
		}
		else if (action.equals("No")) {
			System.out.println("No Button pressed!");
		}
	}

	private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new HandleActionEventsForJButton();

  //Display the window.

  frame.pack();

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

	public static void main(String[] args) {

  //Schedule a job for the event-dispatching thread:

  //creating and showing this application's GUI.

  javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() {

    createAndShowGUI(); 

}

  });
    }

}

 
This was an example on how to handle action events for JButton.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
AlixaProDev
3 years ago

Great Contents. I have also tried the Event handling in java.

Back to top button