• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identifying which button in array is pressed

 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to Java and I'm trying to discover which button in a 3D array is being pressed. TIA of any help. My relevant code below:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyClass {

JPanel[] panels;
JButton[][][] squares = new JButton[2][20][20];


private ButtonListener bl = new ButtonListener();



void addButtons(){
for (int n = 0; n < 2; n++){
panels[n] = new JPanel();
panels[n].setLayout(new GridLayout(20,20));
for (int j = 0; j < 20; j++){
for (int k = 0; k < 20; k++){
squares[n][j][k] = new JButton();
squares[n][j][k].addActionListener(bl);
panels[n].add(squares[n][j][k]);
}
}
}


class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {

// How do i find out which button in squares array was pressed???
// Thanks very much
}
}
}
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

You can set an ActionCommand on each button, identifying what the button is supposed to do. Then in the actionPerformed method of the listener, you can use getActionCommand to get this information from the ActionEvent.

For example...
 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the quick reply Marc.

Sorry for being so thick but do you know how I can actually get a reference to the array - squares[x][y][z]? I'm trying to establish which of 200 buttons is pressed and I'm not sure how to do that based on your answer.

Many thanks again
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you can get a reference to the array from the ActionEvent.

If you're looking for a way to just identify the button, then you can do this by identifying the button in the ActionCommand. For example...

squares[n][j][k] = new JButton();
squares[n][j][k].setActionCommand("I am button: " + n + ", " + j + ", " + k);

Then this information can be retrieved from inside the actionPerformed method, as shown above.

If you really need to reference that array from the listener, one thing you could do is create an instance variable in your listener class (maybe passing the array reference to the constructor). Then you could access the array from inside the ActionPerformed method...

But I'm not sure this is a good idea, because it might complicate what objects are responsible for doing what. In a GUI application, it's a good practice to use a design pattern like Model-View-Controller (MVC).

Basically, the Model is the "logical workings" of your program, and the View is the GUI. The Controller acts as a "go between" connecting the Model and the View. So when a button is pressed in the View, it sends a message to the Controller (a listener). The Controller passes this new information to the Model for processing. Then if the GUI needs to be updated as a result of this processing, then the Model sends a message back to the Controller, telling it to update the View appropriately.

Also see How to use Buttons in the Swing Tutorial.
[ August 25, 2008: Message edited by: marc weber ]
 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Many thanks again for your quick response Marc.

I have just got my code working using your first suggestion. I added...

squares[n][j][k].setActionCommand("" + n + "," + j + "," + k);

... in the loop which added the array of buttons and then stuffed them back into an array like this:

class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String coords = e.getActionCommand();
String[] squarePressed = coords.split(",");

This works fine but it does seem bit clunky. I'll have a look at your other suggestion. I've actually just finished reading 'Head First Design Patterns' as well as "HF Java" (great books for us newbies) and I was planning to use the Model View Controller after getting my code working. (Maybe I should be using it from the start).

Anyway, thanks very much for your help. I would have been lost without it! This is my first post and I'll definately be coming back - David.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it's worth putting in a little time up front with the MVC to have something manageable later. Otherwise, trying to build on it becomes a nightmare.

But you always need a way to know what button is being pressed!
 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ActionEvent (and actually, all EventObjects) has a source which you can get through getSource(). Then loop through your buttons to find it:

But if your array sizes become larger the action command will be faster, since you won't have to loop through the entire 3-dimensional array.
 
David Stevens
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your repy Rob. Yeah, I saw something about e.getSource in 'Thinking in Java 4th ed.' but I couldn't figure out how to obtain the right info. Now I do Thanks very much. - David
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic