setActionCommand() getActionCommand()

SetActionCommand()

This method changes the action command string, but does not affect the string used to label the button.

getActionCommand()

The action command identifies the button. Thus, when using two or more buttons within the same application, the action command gives you an easy way to determine which button was pressed.


Program

vxvc

 

 

 

Program Source

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

class Frame extends JFrame implements ActionListener{
    
    private JButton jb1,jb2,jb3;
    
    Frame()
    {
        setTitle("setActionCommand and getActionCommand");
        setLayout(new FlowLayout());
        setButtons();
        setAction();
        setSize(700, 200);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void setButtons()
    {
        jb1 = new JButton("JButton01");
        jb2 = new JButton("JButton02");
        jb3 = new JButton("JButton03");
        add(jb1);
        add(jb2);
        add(jb3);
    }
    
    private void setAction()
    {
        jb1.addActionListener(this);
        jb1.setActionCommand("jb1");
        jb2.addActionListener(this);
        jb2.setActionCommand("jb2");
        jb3.addActionListener(this);
        jb3.setActionCommand("jb3");
    }
    
    public void actionPerformed(ActionEvent eve)
    {
        if(eve.getActionCommand() == "jb1")
            JOptionPane.showMessageDialog(rootPane, "JButton01 Pressed");
        else if(eve.getActionCommand() == "jb2")
            JOptionPane.showMessageDialog(rootPane, "JButton02 Pressed");
        else if(eve.getActionCommand() == "jb3")
            JOptionPane.showMessageDialog(rootPane, "JButton03 Pressed");
    } 
}

public class Javaapp {
   
    public static void main(String[] args) {
        
        Frame frame = new Frame();
    }
}

Leave a Comment