1

I've created a JPanel[][] Array.

private JPanel[][] pnlFeld;

And filled it with panels

for (int i = 0; i < world.getSize(); i++) {
        for (int j = 0; j < world.getSize(); j++) {
            pnlFeld[i][j] = new JPanel();
            pnlFeld[i][j].setBorder(new EtchedBorder());
            pnlFeld[i][j].addMouseListener(ml);
            pnlFeld[i][j].setBackground(off);
            add(pnlFeld[i][j]);
        }
    }

Now I want to get the array coordinates ([][]) by clicking on them and I have no clue how to do that.

I've only added methods to change the color of the panel I clicked on, nothing related to my problem.

MouseListener ml = new MouseListener() {

        @Override
        public void mouseEntered(MouseEvent e) {
            if (world.getMode().equals("Malen")) {
                if (e.getSource() instanceof JPanel)
                    e.getComponent().setBackground(on);
            //  check();
            } 
            else if (world.getMode().equals("Radieren")) {
                if (e.getSource() instanceof JPanel)
                    e.getComponent().setBackground(off);
            //  check();
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                if (world.getMode().equals("Setzen")) {
                    if (e.getSource() instanceof JPanel) {
                        if (e.getComponent().getBackground() == off) {
                            e.getComponent().setBackground(on);
                        } else
                            e.getComponent().setBackground(off);
                    }
                //  check();
                }
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {}

        @Override
        public void mouseExited(MouseEvent e) {}

        @Override
        public void mouseReleased(MouseEvent e) {}
    };
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rob
  • 11
  • 3
  • post your implementation of `ml` in `addMouseListener` – Blip Jun 02 '15 at 10:01
  • How do you wish to get the coordinates? What I mean to say is that does this panel containing these subpanels has some variable say `x` and `y` that are to be set when any suppanel is clicked? – Blip Jun 02 '15 at 10:27
  • When I click on a panel I want to create a new Point with its coordiantes. – Rob Jun 02 '15 at 10:34
  • Possible [duplicate](http://stackoverflow.com/q/7702697/230513). – trashgod Jun 02 '15 at 10:35
  • do you have a variable say `point` that you would set with this new point you create out of the coordinates? – Blip Jun 02 '15 at 10:40
  • Get the source of MouseEvent and search the array for a match... – MadProgrammer Jun 02 '15 at 10:52

4 Answers4

1

Actually you can use getBounds() to get component location and size. If you mean the array indexes there could be multiple solutions.

  1. Define a Map and place all your panels in the Map with String value e.g. i+":"+j (or define simple pojo class with 2 fields i and j.

  2. Create unique listener for each JPanel to keep the i and j.

  3. Place the panels in a containr with GridBagLayout then you can use gridBagLayoutInstance.getConstraints(theClickedPanel) and check row column of the constraint

StanislavL
  • 56,971
  • 9
  • 68
  • 98
1

Getting JPanel[][] coordinates by clicking on a JPanel

Use a JButton[][] with ActionListener for easier coding and a better user experience. The ActionEvent has a getSource() method that will identify the button that was activated.

This chess GUI uses buttons for the 64 places on the chessboard.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
0

If you create your MouseListener in your loop, you can use i en j within the code of your listener. The drawback is that you'll have a bit more instances of your listener.

0

I could suggest that you set the name of each JPanel as i and j in the loop where you declare and initialise the panels. As illustrated

pnlFeld[i][j].setName(i + "" + j);

And in your mouseClicked event check if the event source is an instance of JPanel and parse the name to get the x and y coordinates like this:

Integer.parseInt(p.getName().subString(0,1))
Integer.parseInt(p.getName().subString(1));
Blip
  • 3,061
  • 5
  • 22
  • 50