How do we show the gridline in GridLayout? in Java?
JPanel panel = new JPanel(new GridLayout(10,10));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
for (int i =0; i<(10*10); i++){
panel.add(new JLabel("Label"));
}
Lines can be addressed using their line number. In a left-to-right language such as English, column line 1 will be on the left of the grid, row line 1 on the top. Lines numbers respect the writing mode of the document and so in a right-to-left language for example, column line 1 will be on the right of the grid.
Definition of grid line : any of a series of numbered horizontal and perpendicular lines that divide a map into squares to form a grid by means of which any point may be located by a system of rectangular coordinates.
I would try to do it by adding borders to the components as they are added. The simple way to do it is just using BorderFactory.createLineBorder()
, like this:
JPanel panel = new JPanel(new GridLayout(10,10));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
for (int i =0; i<(10*10); i++){
final JLabel label = new JLabel("Label");
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.add(label);
}
However, that will give you thicker borders between the cells than at the edges of the panel, because the outer edges will only have a one-pixel thick border and the inside edges will have two one-pixel thick borders together. To work around that, you can use BorderFactory.createMatteBorder()
to only draw one-pixel-wide borders everywhere:
final int borderWidth = 1;
final int rows = 10;
final int cols = 10;
JPanel panel = new JPanel(new GridLayout(rows, cols));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
final JLabel label = new JLabel("Label");
if (row == 0) {
if (col == 0) {
// Top left corner, draw all sides
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
}
else {
// Top edge, draw all sides except left edge
label.setBorder(BorderFactory.createMatteBorder(borderWidth,
0,
borderWidth,
borderWidth,
Color.BLACK));
}
}
else {
if (col == 0) {
// Left-hand edge, draw all sides except top
label.setBorder(BorderFactory.createMatteBorder(0,
borderWidth,
borderWidth,
borderWidth,
Color.BLACK));
}
else {
// Neither top edge nor left edge, skip both top and left lines
label.setBorder(BorderFactory.createMatteBorder(0,
0,
borderWidth,
borderWidth,
Color.BLACK));
}
}
panel.add(label);
}
}
This should give you borders of width borderWidth
everywhere, both between cells and along the outside edges.
There is an easier work around to the thick borders problem mentioned by Joe Carnahan: GridLayout(10,10, -1, -1)
sets the vertical gaps and the horizontal gaps between components to -1. So the full code is:
JPanel panel = new JPanel(new GridLayout(10,10, -1, -1));
panel.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
for (int i =0; i<(10*10); i++){
final JLabel label = new JLabel("Label");
label.setBorder(BorderFactory.createLineBorder(Color.BLACK));
panel.add(label);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With