Question

JPanel btns = new JPanel();
btns.add(new JButton("btn1"));
btns.add(new JButton("btn2"));
btns.setLayout((new FlowLayout(FlowLayout.TRAILING)));
southPanel.add(btns);

Hello, the above, I have provided the code. How would I make a actionPerformed method to certain button. For instance, when I click btn1, it will print out Button 1 is clicked. When I click btn2, it will print out Button 2 is clicked.

Thanks.

Was it helpful?

Solution

Create an instance of object to the Buttons..

JButton button1 = new JButton("btn1");
JButton button1 = new JButton("btn1");

then add them to the Panel

btns.add(button1 );
btns.add(button2 );

add the a action command to the Buttons:

button1.setActionCommand("button1");
button2.setActionCommand("button2");\

create a class that implements ActionListener

private class Controller implements ActionListener

In the actionPerformed get the actionCommand and checks it with the correnponding button:

  @Override
        public void actionPerformed(ActionEvent evt) 
        {
            String actionCommand = evt.getActionCommand(); //get the actionCommand and pass it to String actionCommand

            if(actionCommand.equals("button1"))
                           //button 1 clicked
            else if(actionCommand.equals("button2"))
                          //button 1 clicked
}

OTHER TIPS

You simply add two different ActionListener to the two buttons.

btn1.addActionListener(new ActionListener(){.....});
btn2.addActionListener(new ActionListener(){.....});

You need to have a handle to the buttons, and .add(new JButton("btn1")) doesn't allow you to access them easily.

Declare your buttons as variables, as in JButton button = new JButton("btn1"). Then, call their .addActionListener(...) method to define the actionListener to use.

Assuming that you want two different actionListeners, you can use the following.

button.addActionListener(new ActionListener() { 
        public void actionPerformed() {...} 
    } 
);

If you don't care about the buttons after they have an actionListener, you may use a temporary variable:

JButton temp = new JButton("btn1");
temp.addActionListener(...);
btns.add(temp); //adds btn1

temp = new JButton("btn2");
temp.addActionListener(...);
btns.add(temp); //adds btn2

Finally, if you want to use one actionListener to handle both buttons, use the .setActionCommand() method to give a unique command to each button. Then, in actionPerformed, use an if statement and check e.getActionCommand.equals("myButton'sAction"). For example:

JButton temp = new JButton("btn1");
temp.addActionListener(this);
temp.setActionCommand("Btn1");
btns.add(temp); //btn1

temp = new JButton("btn2");
temp.addActionListener(this);
temp.setActionCommand("Btn2");
btns.add(temp); //btn2

public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    if (e.getActionCommand.equals("Btn1"))
        System.out.println("Button 1 pressed");
    if (e.getActionCommand.equals("Btn2"))
        System.out.println("Button 2 pressed");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top