JButton

Set action command for JButton

In this example we are going to see how to set action command for JButton component. This is a very important task when working in a Java GUI application, as most of the times you have to bundle a button with a specific action.

In short, to set action command for JButton one should follow these steps:

  • Create a class that extends JFrame and implements ActionListener.
  • Create a new JButton.
  • Use JButton.addActionListener to add a specific ActionListener to this component.
  • Use JButton.setActionCommand to add a specific command to this component.
  • Override actionPerformed method and use ActionEvent.getActionCommand to get the command of the button. Then you can customize this command as you wish.

Let’s see the code:

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 SetActionCommandForJButton extends JFrame implements ActionListener {

	private static final long serialVersionUID = 1L;

	public SetActionCommandForJButton() {

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

		JButton button = new JButton("Java Code Geeks - Java Examples");

		//add Button
		add(button);

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

		// define a custom short action command for the button
		button.setActionCommand("Geeks");

		// add button to frame
		add(button);

	}

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

	private static void createAndShowGUI() {

  //Create and set up the window.

  JFrame frame = new SetActionCommandForJButton();

  //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 set the action command 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.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KateHikes1488
KateHikes1488
2 years ago

nice

Last edited 2 years ago by KateHikes1488
Alexandre
Alexandre
1 year ago

Why are you adding the the button twice to the frame (in lines 22 and 31)?

public class SetActionCommandForJButton extends JFrame implements ActionListener {
 
    private static final long serialVersionUID = 1L;
 
    public SetActionCommandForJButton() {
 
        // set flow layout for the frame
        this.getContentPane().setLayout(new FlowLayout());
 
        JButton button = new JButton("Java Code Geeks - Java Examples");
 
        //add Button
        add(button);
 
        //set action listeners for buttons
        button.addActionListener(this);
 
        // define a custom short action command for the button
        button.setActionCommand("Geeks");
 
        // add button to frame
        add(button);
buttontwice.png
Back to top button