-1

I created a Gridlayout out of Buttons with an two-dimensional array. When I press the button I want to execute a method from another class which needs the coordinates of the button which has been pressed. I want to save these numbers in a array.

int buttonCoordinates[2]

I already created an actionlistener

jBTN_field[y][x].addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent pE) {

    }
}

I want to save the coordinates in an Array.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Samuel
  • 356
  • 2
  • 13
  • Why do you need the coordinates? That array is going to store a single number, are you sure it's what you want? And what have you tried? That empty `ActionListener` tells us nothing. – Frakcool May 06 '19 at 15:21
  • @Frakcool It should store 2 numbers. x and y. – Samuel May 06 '19 at 15:23
  • Ok, I get your point. And again, what have you tried? Please post a proper [mcve] that shows what you've tried, and what are the issues you have. – Frakcool May 06 '19 at 15:24
  • Just a suggestion, so that you can improve your code. Why not make a class `class BtnCoordinates`, which will have 2 variables: `int x, y;`. Then create an array of BtnCoordinates: `BtnCoordinates location = new BtnCoordinates[15];`. Therefore, you will be able to access the coordinates you want -> `location[10].x`. But this is just an example, and you should rething the way you designed your code. I suggest not using a 2D array – Soutzikevich May 06 '19 at 15:26
  • @Frakcool In actionPerformed I want to execute a method with the array as parameters. The method needs the location of the button. – Samuel May 06 '19 at 15:27
  • 1
    @the_hashtag Additionally, If you want to just pass x and y as parameters, then why are you using an anonymous inner class? Create a "normal" class that implements ActionListener, and Override actionPerformed method, to add the parameters Actionevent e, int x, int y. Then you can pass the new class you created as a parameter when you initialize your buttons. – Soutzikevich May 06 '19 at 15:30
  • 1
    Again, I understand what you're trying to do, but I think your approach is not the best as @P.Soutzikevich has already pointed out, you might need to change the way you're trying to do it. A 1D / 2D array is not what I would do. Simply call `yourMethod(buttonCoordinates)` and you'll be passing your array as argument, if you don't know how to fill the array: `buttonCoordinates[0] = y` and `buttonCoordinates[1] = x`. BTW why do you have `y` first and then `x`? It seems wrong. If that doesn't solve your question, post a proper [mcve] – Frakcool May 06 '19 at 15:34
  • Possible duplicate of [How to make a method that disables JButton?](https://stackoverflow.com/questions/49675729/how-to-make-a-method-that-disables-jbutton) – Frakcool May 06 '19 at 15:35
  • @Frakcool I want to make the grid from left to right – Samuel May 06 '19 at 15:41
  • You're accessing your `JButton`s in a different way if you're accessing `[y][x]` instead of `[x][y]`. Check the duplicate question and the accepted answer. It contains a [mcve] that you can copy-paste and see the same program there. Then just modify it to fit your needs. Without more information we can't provide more help and you seem to only answer to last question asked, so, you're on your own now unless you take the [tour], go to [ask] and post a MCVE – Frakcool May 06 '19 at 15:44

1 Answers1

1

Without attempting to change the structure of your program so far, here is a "naive" approach.

Instead of using an Anonymous Inner Class, you could create another class:

public class MyListener implements ActionListener {

    private int x;
    private int y;

    public MyListener(int x, int y){
        this.x = x;
        this.y = y;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //do something with this.x
        //do something with this.y
    }
}

and then you can adapt this to your code:

jBTN_field[y][x].addActionListener(new MyListener(x, y));

Now, for accessing the coordinate of a button, it will be as simple as:

button[y][x].

NOTE

Your code is not logically wrong. I'm just showing you an alternative, that could simplify things for you, so you can solve your problem more methodically. The example I have showed, is appropriate if you want to have the same actionPerformed() functionality for all buttons. Otherwise, an anonymous inner class is more appropriate.

Alternatively, should you wish to keep your code as is, you can do:

jBTN_field[y][x].addActionListener(new ActionListener() {
    int x = "value_for_x", y = "value_for_y";

    @Override
    public void actionPerformed(ActionEvent pE) {
        //do something with x
        //do something with y
    }
}
Soutzikevich
  • 991
  • 3
  • 13
  • 29