Java getActionCommand() Method

Sheeraz Gul Sep 05, 2022
Java getActionCommand() Method

This tutorial demonstrates how to use getActionCommand() method in Java.

Use the getActionCommand() Method in Java

The getActionCommand() method from the ActionListener class identifies a button. When there are multiple buttons, the getActionCommand() gives us an easy way to know which button was pressed.

The getActionCommand() method will give us string representation in action command. The value will be component specific which we can set using the setActionCommmand() method.

Let’s create an example using the getActionCommand method. First, create a class that extends JFrame and implements ActionListener.

Then, initialize private Buttons and use the constructor method to set title, size, setButtons(), setActions() methods, etc. Use the setButtons() method to assign names to the buttons and add them and use the setActions() method to set the actions commands which will be invoked when the getActionCommand is invoked.

Now, use the actionPerformed to assign actions to the above commands. These commands will be invoked using the getActionCommand().

Lastly, create another class with the main method and create an instance of the above class to run the application.

Complete Source Code:

package delftstack;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

class DemoFrame extends JFrame implements ActionListener{

    private JButton J_Button1,J_Button2,J_Button3;

    DemoFrame() {
        setTitle("getActionCommand");
        setLayout(new FlowLayout());
        setButtons();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    private void setButtons() {
        J_Button1 = new JButton("JButton One");
        J_Button2 = new JButton("JButton Two");
        J_Button3 = new JButton("JButton Three");
        add(J_Button1);
        add(J_Button2);
        add(J_Button3);
    }

    private void setAction() {
        J_Button1.addActionListener(this);
        J_Button1.setActionCommand("J_Button1");
        J_Button2.addActionListener(this);
        J_Button2.setActionCommand("J_Button2");
        J_Button3.addActionListener(this);
        J_Button3.setActionCommand("J_Button3");
    }

    public void actionPerformed(ActionEvent event) {
        if(event.getActionCommand() == "J_Button1")
            JOptionPane.showMessageDialog(rootPane, "The JButton One is Pressed");
        else if(event.getActionCommand() == "J_Button2")
            JOptionPane.showMessageDialog(rootPane, "The JButton Two is Pressed");
        else if(event.getActionCommand() == "J_Button3")
            JOptionPane.showMessageDialog(rootPane, "The JButton Three is Pressed");
    }
}

public class Example {
    public static void main(String[] args) {
        DemoFrame Demo_Frame = new DemoFrame();
    }
}

The code above will create three buttons and set action commands for them. When we press a button, the getActionCommand() will be invoked, and the method showMessageDialog() will show the corresponding message.

Output:

Use getActionCommand() Method in Java

Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - Java Command