0

Suppose I have an array of buttons

private JButton[] myButtons = new JButton[5];

for (int i=0; i<5; i++)
{
    myButtons[i] = new JButton(Integer.toString(i));
    myButtons[i].setSize(50, 50);
    panel.add(myButtons[i]);
}

How can I add a listener to these buttons so that when I click on one of them, I know at which array position index it is at?

2 Answers 2

2

You kind of don't care, start by adding an ActionListener to the buttons

myButtons[i].addActionListener(this); // Or some other ActionListener

In the actionPeformed method, you can look up which button it is using the ActionEvent#getSource

@Override
public void actionPerformed(ActionEvent evt) {
    for (JButton btn : myButtons) {
        if (btn.equals(evt.getSource()) {
            // Do what ever you need
            break;
        }
    }
}

You can also use the actionCommand property of the JButton

for (int i=0; i<5; i++)
{
    myButtons[i] = new JButton(Integer.toString(i));
    myButtons[i].setActionCommand("button " + i);
    myButtons[i].addActionListener(this);
    panel.add(myButtons[i]);
}

And when actionPeformed is called, use ActionEvent#getActionCommand to figure out which button was pressed.

A better idea might be to create a dedicated ActionListener for each button...

public class ButtonActionHandler implements ActionListener {
    private final JButton button;
    public ButtonActionHandler(JButton button) {
        this.button = button;
    }

    public void actionPerformed(ActionEvent evt) {
        // Do what ever you need to do with the button...
    }
}

for (int i=0; i<5; i++)
{
    myButtons[i] = new JButton(Integer.toString(i));
    myButtons[i].addActionListener(new ButtonActionHandler(myButtons[i]));
    panel.add(myButtons[i]);
}

Another idea would be to make use of the Action API which would allow you to define a self contained entity which was capable of configuring the button and handling the associated action event by itself. See How to Use Actions for more details

But which you might use will come down to why you need to identify the buttons in the first place.

Sign up to request clarification or add additional context in comments.

3 Comments

can you explain "myButtons[i].addActionListener(this);" better cuz personally it makes confuse. how this is gonna recongize the right thing?
plus one you bc of the last idea which is awesome
@KickButtowski You would either use ActionEvent#getSource and compare it to the buttons array or use the actionCommand (preferably) property of the button and ActionEvent
0

You can add listener in loop, like below if you implement ActionListener interface with class. For example,

class TestGUI extends JPanel implements ActionListener{
 public TestGUI(){
  for(int i=0; i< 5; i++){
  ....
  myButtons[i].addActionListener(this);
 }
}

or, if you have separate Listener class or method.

myButtons[i].addActionListener(new MyListener());

Than at actionPerformed method you can check with button is clicked,

public void actionPerformed(ActionEvent e){
   if("0".equals(e.getActionCommand())){
     System.out.println("First button is clicked");
   }
   ... so on
}

3 Comments

And getting the array index of the button that was clicked?
I am sure that part is easy to figure it out do not you agree?
@Eclipse22, getActionCommand will give you the text of Button. In your case it will be "0", "1"..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.