I was hoping to make the grid produced by the code below report x and y coordinates of the location of the mouse pointer relative to pnlGrid
when clicked. But wherever I clicked (before i added a JLayeredPane
), I got no coordinates except for when the mouse pointer was in the red area.
So I added several lines of code to make a JLayeredPane
and I get mouse coordinate output, but no grid lines as shown in second screen shot.
How do I get both grid lines AND report of mouse coordinates when any place in the grid is clicked?
package gridcellcoordinator;
import gbl.GBConstraints;
import static java.awt.Color.*;
import java.awt.Color;
import java.awt.Dimension;
import static java.awt.EventQueue.invokeLater;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import static javax.swing.JLayeredPane.DEFAULT_LAYER;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.event.MouseInputAdapter;
public class GridCellCoordinator {
final static int
GRID_PANEL_BORDER_WIDTH = 5,
N = 11,
CELLSIZE = 40;
//static final JLayeredPane layer = new JLayeredPane();
static final JPanel panel = new JPanel();
static final int SM_CELL_BORDER_WIDTH = 1;
static LineBorder SMcellBorder = new LineBorder(BLACK,SM_CELL_BORDER_WIDTH);
static JTextField[][] cells = new JTextField[N][N];
static JFrame frame = new JFrame();
public GridCellCoordinator(){
makeGrid();
}
private void makeGrid(){
JPanel pnlGrid = new JPanel();
pnlGrid.setLayout(new GridLayout(N,N));
pnlGrid.setBackground(BLUE);
pnlGrid.setBorder(BorderFactory.createLineBorder(Color.red,GRID_PANEL_BORDER_WIDTH));
pnlGrid.setLayout(new GridLayout(N, N));
for(int i = 0 ; i < N ; i++)
for(int j = 0; j < N; j++){
cells[i][j] = new JTextField();
cells[i][j].setText("X");
cells[i][j].setPreferredSize(new Dimension(CELLSIZE,CELLSIZE));
cells[i][j].setHorizontalAlignment(JTextField.CENTER);
cells[i][j].setFocusTraversalKeysEnabled(false);
cells[i][j].setBorder(SMcellBorder);
cells[i][j].setOpaque(true);
pnlGrid.add(cells[i][j], new GBConstraints());
}
panel.addMouseListener(new MouseInputAdapter()
{
public void mousePressed (MouseEvent e){panelMousePressed (e);}
});
pnlGrid.setPreferredSize(new Dimension(N*(CELLSIZE + 1) + 2*GRID_PANEL_BORDER_WIDTH ,
N*(CELLSIZE + 1) + 2*GRID_PANEL_BORDER_WIDTH));
panel.add(pnlGrid);
panel.setVisible(true);
panel.setOpaque(true);
panel.setPreferredSize(pnlGrid.getPreferredSize());
panel.setVisible(true);
//layer.add(pnlGrid, DEFAULT_LAYER);
//layer.setPreferredSize(pnlGrid.getPreferredSize());
//layer.setVisible(true);
frame.add(panel);
frame.setSize(new Dimension(pnlGrid.getPreferredSize()));
frame.setVisible(true);
frame.pack();
System.out.println("pnlGrid component count: " + pnlGrid.getComponentCount());
System.out.println("computed dimension: " + (N*(CELLSIZE + 1) + 2*GRID_PANEL_BORDER_WIDTH));
System.out.println("pnlGrid pref size: " + pnlGrid.getPreferredSize());
System.out.println("layer pref size: " + layer.getPreferredSize());
System.out.println("panel pref size: " + panel.getPreferredSize());
}
public void panelMousePressed(MouseEvent e){
JOptionPane.showMessageDialog(null,"Pressed:" + e.getX() + " " + e.getY());
}
public static void main(String[] args) {
invokeLater(new Runnable() {
public void run() {
new GridCellCoordinator();
}
});
}
}
P.S. As is, the code produces the grid on the left but only gives coordinates inside the red border. Remove the comment bars on the 4 layer lines to get the coordinates but no gridlines. I'd like BOTH gridlines and coordinates.