JButton submitButton = new JButton("Submit"); submitButton.setActionCommand("submitClicked");
JButton button1 = new JButton("Button 1"); button1.setActionCommand("button1Clicked"); JButton button2 = new JButton("Button 2"); button2.setActionCommand("button2Clicked"); ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { String actionCommand = e.getActionCommand(); if (actionCommand.equals("button1Clicked")) { // handle button 1 click } else if (actionCommand.equals("button2Clicked")) { // handle button 2 click } } }; button1.addActionListener(listener); button2.addActionListener(listener);In both examples, the setActionCommand method is called on a JButton object to set the action command string associated with the button. This action command string is then used to determine which button was clicked in an ActionListener. The java.awt package library is used in both examples.