I have two tables and two buttons. I want table-1 to be in the first column and as wide as 3 units, then I want the two buttons to the right of the table much more narrower (1 units in width), I want button one on the top and button2 on the bottom. Now to the right of these buttons I want the other table (table-2) again as wide as 3 units. To achieve this I have used the following constraints:
//table1
c = new GridBagConstraints(0, 0, 3, 2, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
c.fill = GridBagConstraints.BOTH;
outerPanel.add(all_app_scrollpane, c);
//button1
c = new GridBagConstraints(3, 0, 1, 1, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
outerPanel.add(addButton, c);
//button2
c = new GridBagConstraints(3, 1, 1, 1, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
outerPanel.add(removeButton, c);
//table2
c = new GridBagConstraints(4, 0, 3, 2, 1, 1,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
new Insets(0, 0, 0, 0), 0, 0);
c.fill = GridBagConstraints.BOTH;
outerPanel.add(curr_app_scrollpane, c);
Let's look at the GridBagCOnstraints of table1. As far as I know, the first parameter states that it will start from the 0. column and will be 3 columns wide (third parameter). And then button1 will start from the third column and will be one column wide. But I think I know something wrong as the result is very different then I expect.
I want these buttons much more narrower and tables much wider. How can I do that? I want to accomplish this using GridBag, as this small panel is just a small part of a very complex gui, and the whole gui is designed using gridbag. So I don't want to change the coding style.
You have to set the grow settings (weightx) of the layout so that only the left and right column are growing.
Like this:
contentPane.setLayout(new GridBagLayout());
((GridBagLayout)contentPane.getLayout()).columnWidths = new int[] {0, 0, 0, 0};
((GridBagLayout)contentPane.getLayout()).rowHeights = new int[] {0, 0, 10, 0, 0, 0};
((GridBagLayout)contentPane.getLayout()).columnWeights = new double[] {1.0, 0.0, 1.0, 1.0E-4};
((GridBagLayout)contentPane.getLayout()).rowWeights = new double[] {1.0, 0.0, 0.0, 0.0, 1.0, 1.0E-4};
//---- leftBtn ----
leftBtn.setText("left Button");
contentPane.add(leftBtn, new GridBagConstraints(0, 0, 1, 5, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
//---- rightBtn ----
rightBtn.setText("right Button");
contentPane.add(rightBtn, new GridBagConstraints(2, 0, 1, 5, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
//---- addBtn ----
addBtn.setText("add Button");
contentPane.add(addBtn, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
//---- remBtn ----
remBtn.setText("rem Button");
contentPane.add(remBtn, new GridBagConstraints(1, 3, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
So it gives that:
Greetings Florian
Edit: Here a Screenshot from the IDE, with additional space between the buttons: