GridLayout struggle when it has excess rows and how to overcome

In my Java code, I am using GridLayout (line shown in red) in code below:

frame = new JFrame("Contacts Details");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
buttonPanel = new JPanel();
mainPanel.setLayout(new GridLayout(9, 3));
buttonPanel.setLayout(new GridLayout(1, 3));

The result was this:

Name
[                        ] (Text Box for name)
City
[                        ] (Text Box for City)
Cell
[                        ] (Text Box for Phone)

whereas, I expected to get:

Name [                        ] (Text Box for name)
City [                        ] (Text Box for City)
Cell [                        ] (Text Box for Phone)

If you want to get 3 rows x 3 columns in the GridLayout, you should changed the GridLayout statement to that shown in blue below:

frame = new JFrame("Contacts Details");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
buttonPanel = new JPanel();
mainPanel.setLayout(new GridLayout(3, 3));
buttonPanel.setLayout(new GridLayout(1, 3));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s