GridLayout + Mouse Listener

Ok guys, I have a problem, I don’t know how to find out which cell was clicked on the grid layout, is there any function?

I have a grid layout in the container for 10 rows and 10 columns, and I want the mouse listener to accept all cells, so when I click on cell (2,1), I would say which cell I click, because mouse listener.

Any clues? thanks in advance in advance

+5
source share
3 answers

Add a MouseListener to the container that uses the GridLayout and which contains the components in the grid. Then on mousePressed, use a MouseEvent object called myMouseEvent to get the click point and call getComponentAt(myMouseEvent.getPoint);to get the component with the click. No fuss.

For instance:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;

public class TestComponentAt extends JPanel {
   private static final int ROW_COUNT = 10;
   private static final int W = 60;
   private static final int H = W;
   private static final Dimension PREF_SIZE = new Dimension(W, H);
   protected static final Color SELECTION_COLOR = Color.pink;
   private JPanel selectedPanel = null;
   private Color originalColor = null;

   public TestComponentAt() {
      setLayout(new GridLayout(ROW_COUNT, ROW_COUNT, 1, 1));
      setBackground(Color.black);
      for (int i = 0; i < ROW_COUNT * ROW_COUNT; i++) {
         JPanel panel = new JPanel();
         String name = String.format("[%d, %d]", 
               i / ROW_COUNT, i % ROW_COUNT);
         panel.setName(name);
         if (i == 0) {
            originalColor = panel.getBackground();
         }
         panel.setPreferredSize(PREF_SIZE);
         add(panel);
      }
      addMouseListener(new MouseAdapter() {
         @Override
         public void mousePressed(MouseEvent e) {
            JPanel panel = (JPanel) getComponentAt(e.getPoint());
            if (panel == null || panel == TestComponentAt.this) {
               return;
            }
            if (selectedPanel != null) {
               selectedPanel.setBackground(originalColor);
               selectedPanel.removeAll();
               selectedPanel.revalidate();
               selectedPanel.repaint();
            }
            selectedPanel = panel;
            selectedPanel.setBackground(SELECTION_COLOR);
            selectedPanel.add(new JLabel(selectedPanel.getName()));
            selectedPanel.revalidate();
            selectedPanel.repaint();
         }
      });
   }

   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestComponentAt");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestComponentAt());
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
+8
source

, (2,1), , 100 , = 10 * 10, (2,1) x = 10, y = 0 getX() getY() mouseListener, , , x y (2,1), , (2,1). enter image description here

0

recursively add mouselistener to all components of your user interface and debug ...

-1
source

All Articles