wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Identifying which button in array is pressed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Identifying which button in array is pressed" Watch "Identifying which button in array is pressed" New topic
Author

Identifying which button in array is pressed

David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 25, 2008
Posts: 19
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Stevens
Greenhorn

Joined: Aug 25, 2008
Posts: 19
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
 
GeeCON Prague 2014
 
subject: Identifying which button in array is pressed