Java / Swing: trying to get BorderLayout to play well with GridBagLayout

I would like to have a window in which there are 3 menus, one tied to the left, the other tied to the center, and the last to the right. Like this:

-------------------------------------------- -toolbar1---------toolbar2---------toolbar3- -------------------------------------------- - - - rest of the window does something here - 

The problem I am facing is what I get:

 -------------------------------------------- ---------toolbar1toolbar2toolbar3----------- -------------------------------------------- - - - rest of the window does something here - 

Here is a sample code (compiles and shows the problem):

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TestClass extends JFrame { public TestClass() { super("test"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); final JPanel upper = new JPanel(); upper.setLayout(new GridBagLayout()); final GridBagConstraints gbc = new GridBagConstraints(); final JButton toolbar1 = new JButton("toolbar1"); final JButton toolbar2 = new JButton("toolbar2"); final JButton toolbar3 = new JButton("toolbar3"); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; upper.add(toolbar1, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.CENTER; upper.add(toolbar2, gbc); gbc.gridx = 2; gbc.anchor = GridBagConstraints.EAST; upper.add(toolbar3, gbc); add(upper, BorderLayout.NORTH); final JPanel something = new JPanel(); something.setBackground(Color.WHITE); something.setPreferredSize(new Dimension(600, 600)); something.repaint(); add(something, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { final TestClass test = new TestClass(); } } 

How can i fix this? I thought that by setting the anchor in the GridBagConstraints I would take care of this, but that did not work.

+4
source share
2 answers

You forgot to add:

 gbc.weightx = 1.0; gbc.weighty = 1.0; 

Your modified code should look like this:

 import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class TestClass extends JFrame { public TestClass() { super("test"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); final JPanel upper = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); upper.setLayout(gridbag); GridBagConstraints gbc = new GridBagConstraints(); final JButton toolbar1 = new JButton("toolbar1"); final JButton toolbar2 = new JButton("toolbar2"); final JButton toolbar3 = new JButton("toolbar3"); gbc.gridx = 0; gbc.gridy = 0; gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.anchor = GridBagConstraints.WEST; upper.add(toolbar1, gbc); gbc.gridx = 1; gbc.anchor = GridBagConstraints.CENTER; upper.add(toolbar2, gbc); gbc.gridx = 2; gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; upper.add(toolbar3, gbc); add(upper, BorderLayout.NORTH); final JPanel something = new JPanel(); something.setBackground(Color.WHITE); something.setPreferredSize(new Dimension(600, 600)); something.repaint(); add(something, BorderLayout.CENTER); pack(); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] args) { final TestClass test = new TestClass(); } } 

He works.

+1
source

If your toolbar looks like BorderLayout (WEST, CENTER, EAST), why not use BorderLayout instead of GridBagLayout?

In any case, if you insist on using a GridBagLayout, set the weightx limit for toolbar2 to 1. This tells the layout manager that if more space is available, it should provide all of this for toolbar2.

  gbc.weightx = 1; upper.add(toolbar2, gbc); gbc.weightx = 0; 
+1
source

All Articles