This example use the GridLayout
to create a four by four grid layout. One each of the grid we place a JTextField
component to read user inputs.
package org.kodejava.example.swing;
import javax.swing.*;
import java.awt.*;
public class GridLayoutDemo extends JFrame {
public GridLayoutDemo() {
initializeUI();
}
private void initializeUI() {
setSize(300,300);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Create a four by four GridLayout with 5 pixel horizontal and vertical
// gap between each row or column.
GridLayout grid = new GridLayout(4, 4, 5, 5);
//grid.setColumns(4);
//grid.setRows(4);
//grid.setHgap(5);
//grid.setVgap(5);
setLayout(grid);
JTextField[][] textFields = new JTextField[4][4];
for (int i = 0; i < textFields.length; i++) {
for (int j = 0; j < textFields[i].length; j++) {
textFields[i][j] = new JTextField(String.valueOf((i + 1) * (j + 1)));
// Put the text at the center of the JTextField component
textFields[i][j].setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(textFields[i][j]);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new GridLayoutDemo().setVisible(true);
}
});
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. You can support my works by donating here. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I clear the current command line in terminal? - February 14, 2019
- How do I generate random alphanumeric strings? - February 7, 2019
- Why do I get ArrayIndexOutOfBoundsException in Java? - January 29, 2019