I have went through many answers here and tried implementing the solutions but, my code still won't work as intended... Basically I have a lot of buttons, the size is fixed, so I get the row and column by dividing the relaive X and Y of the buttons. Than I need to send the data to a nother proram. It works with the buttons, but not with the mouse click. I'll give you my code and a dummy main, so you can try it out. Both ways to get the mouse listener working are still there. The first is commented out like /* */ and // The forwarding code was also removed, to keep this to a working minumum.
Thank you for your time.
import java.awt.GridLayout; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.awt.event.MouseAdapter; import javafx.scene.input.MouseEvent; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; @SuppressWarnings("serial") public class DoorMatrix extends JPanel { static JFrame f = new JFrame(); private static JButton[][] buttons; int x = 0; int y = 0; public DoorMatrix(int row, int col) { super(new GridLayout(row, col)); buttons = new JButton[row][col]; for (int i = 0; i < buttons.length; i++) { for (int j = 0; j < buttons[i].length; j++) { final int curRow = i; final int curCol = j; buttons[i][j] = new JButton((j + 1) + ", " + (i + 1)); buttons[i][j].addKeyListener(enter); buttons[i][j].addMouseListener(new A2()); // buttons[i][j].addMouseListener(pressedit); // addMouseListener(pressedit); buttons[i][j].addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: if (curRow > 0) buttons[curRow - 1][curCol].requestFocus(); break; case KeyEvent.VK_DOWN: if (curRow < buttons.length - 1) buttons[curRow + 1][curCol].requestFocus(); break; case KeyEvent.VK_LEFT: if (curCol > 0) { if (curCol != 5) { buttons[curRow][curCol - 1].requestFocus(); } else buttons[curRow][curCol - 2].requestFocus(); } break; case KeyEvent.VK_RIGHT: if (curCol < buttons[curRow].length - 1) { if (curCol != 3) { buttons[curRow][curCol + 1].requestFocus(); } else buttons[curRow][curCol + 2].requestFocus(); } break; default: break; } // end of switch } }); add(buttons[i][j]); } } } private KeyListener enter = new KeyAdapter() { @Override public void keyTyped(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) { ((JButton) e.getComponent()).doClick(); y = ((JButton) e.getComponent()).getX(); x = ((JButton) e.getComponent()).getY(); if (x != 0) {x = (x / 26);} x++; if (y != 0) {y = (y / 54);} y++; System.out.print(y + ". module " + x + ". door \n"); x = 0; y = 0; } if (e.getKeyChar() == KeyEvent.VK_ESCAPE) {f.dispose();} } }; /* private MouseListener pressedit = new MouseAdapter() { public void mouseClicked(MouseEvent m) { if (m.getEventType() == MouseEvent.MOUSE_PRESSED) { ((JButton) m.getSource()).doClick(); y = ((JButton) m.getSource()).getX(); x = ((JButton) m.getSource()).getY(); if(x != 0){x = (x / 26);} x++; if(y != 0){y = (y / 54);} y++; System.out.print(y + ". module " + x + ". door \n"); x =0; y =0;} }}; */ public class A2 extends MouseAdapter { public void MousePressed(MouseEvent e) { ((JButton) e.getSource()).doClick(); y = ((JButton) e.getSource()).getX(); x = ((JButton) e.getSource()).getY(); if (x != 0) {x = (x / 26);} x++; if (y != 0) {y = (y / 54);} y++; System.out.print(y + ". module " + x + ". door"); x = 0; y = 0; } // end of event public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} } // end of adapter public static void main(String[] args) { int a = 0; int b = 7; f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new DoorMatrix(12, 9)); while (a < 2) { while (b < 12) { buttons[b][a].setVisible(false); b++; } a++; b = 7; } a = 4; b = 0; while (b < 12) { buttons[b][a].setVisible(false); b++; } b = 7; a = 7; while (a < 9) { while (b < 12) { buttons[b][a].setVisible(false); b++; } a++; b = 7; } f.pack(); f.setVisible(true); f.setResizable(false); } }
KeyListenerwith buttons, 2- Don't useActionListenerwith buttons...use aActionListener