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

  

  

  

Blue Captcha Image
Refresh

*

September 2019
M T W T F S S
« Aug    
 1
2345678
9101112131415
16171819202122
23242526272829
30