1

I'm new in Java.

I'm making Mines. I'm using GridLayout with paramteres (x_length, y_length). I'd like know which button I pressed -> his coordinates (x,y). But if I type it to listener it gives me error -> change modifier of 'x' to final. So my ask is how can I simply get coordinates of buttons?

And I also want to ask how can I simply change size of buttons? setSize does not work for me.

    for (int y = 0; y < y_length; y++)
    {
        for (int x = 0; x < x_length; x++)
        {               
            buttons[x][y] = new JButton("X");

            buttons[x][y].addMouseListener(new MouseAdapter()
            {
                public void mouseClicked(MouseEvent e)
                {
                    if (e.getButton() == MouseEvent.BUTTON1)
                    {
                        //exception -> Cannot refer to a non-final variable x inside an inner class defined in a different method
                        JOptionPane.showMessageDialog(null, "Left -> " + x + " | " + y);
                    }
                    else if (e.getButton() == MouseEvent.BUTTON3)
                    {
                        JOptionPane.showMessageDialog(null, "Right -> " + x + " | " + y);
                    }
                }
            });
            mines_array.add(buttons[x][y]);
        }
    }
sczdavos
  • 2,035
  • 11
  • 37
  • 71
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) It is best to ask one question per ..question. 3) The layout manager will generally honor the preferred size of a component over the size. 4) Please copy/paste error messages and exception output and use code formatting on it. 5) It is possible that an `ActionListener` would be a better fit for the buttons in this GUI. What does it do? – Andrew Thompson Mar 22 '12 at 09:19
  • You can't make x and y `final` as they constantly change in the loop. What you can do (but it is an ugly hack, that is why I don't put it as an answer), is put, inside the nested loop, a declaration: `final int x2 = x; final int y2=y` and refer to `x2` and `y2` inside your listener. `ActionListener`is more appropriate and set the preferredSize instead of the size, as Andrew just said. – Guillaume Polet Mar 22 '12 at 09:22

2 Answers2

2

You should first create a custom class for your listener instead of an anonymous one, since it needs parameters (x and y).

private static class ButtonMouseListener extends MouseAdapter {
    private final int x;
    private final int y;

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

    public void mouseClicked(MouseEvent e) {
        if (e.getButton() == MouseEvent.BUTTON1) {
            JOptionPane.showMessageDialog(null, "Left -> " + x + " | " + y);
        }  else if (e.getButton() == MouseEvent.BUTTON3) {
            JOptionPane.showMessageDialog(null, "Right -> " + x + " | " + y);
        }
    }
}

Then, you can use your code like this:

for (int y = 0; y < y_length; y++) {
    for (int x = 0; x < x_length; x++) {               
        buttons[x][y] = new JButton("X");
        buttons[x][y].addMouseListener(new ButtonMouseListener(x, y));
        mines_array.add(buttons[x][y]);
    }
}

That's all, have fun. About the size of the buttons, if you use a layout in their parent container, their size will be automatically computed by the layout and cannot be changed with setSize().

Aurelien Ribon
  • 7,548
  • 3
  • 43
  • 54
2
  1. for Mines is better use JToggleButtons

  2. put there ClientProperty

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319