简体   繁体   中英

Detect which button was clicked in a 2D array Java

In our code we have a 10 by 10 button array. We made the 10 by 10 array using a nested for loop, and we have no issue creating the buttons. Also, we have it so that when a button a is clicked it displays "Button Clicked". But how can we identify which button was clicked?

We're using actionListeners and actionPerformed methods.

You can call the getSource() method on the event.

Or you can use Action classes in your buttons and create a new instance of each when you build the buttons.

Put all the buttons in a list (easily accomplished in the inner loop), make the list available to the ActionListener (eg. as a property of the outer class; I do not know how your numerous team arranged the listeners, so I cannot provide any details). Then call:

  int buttonIndex = listWithButtons.indexOf(event.getSource())

If one of you wants to know the exact coordinates of the button, they can be calculated by the formulas:

int row = buttonIndex / 10;
int col = buttonIndex % 10;

I'm assuming this is a JButton. You can use setActionCommand("command" + row + "-" + column). Then in the listener just say getActionCommand() to see which button was clicked.

Two simples solutions, but probably not the best ones :

The button class implementing its own listener.

You can also just test each button to see if it equals the action.getSource() object. Or simply cast (ButtonClass) to the getSource() to be able to use the retrieved button.

You can use getActionCommand just like this:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TestButtons extends JPanel {

    private static final long serialVersionUID = 1L;

public TestButtons() {

    JButton btn1 = new JButton("Btn1");
    btn1.addActionListener(new ButtonListener());
    add(btn1);
    JButton btn2 = new JButton("Btn2");
    btn2.addActionListener(new ButtonListener());
    add(btn2);

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new TestButtons());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}

class ButtonListener implements ActionListener {
  ButtonListener() {
  }

  public void actionPerformed(ActionEvent e) {
      System.out.println(e.getActionCommand()+ " has been clicked");
  }
}

We ended up using a nested for loop inside of actionPerformed that ran through the 2d array and called the action methods from there. It probably isn't the best solution, and it's probably best to use a different technique, but it seems to work just fine.

Though there are some really nice ideas in here, thank you guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM