JSeparator will not show using GridBagLayout

I want to add a vertical JSeparator between two components using a GridBagLayout. The code I have is as follows:

public MainWindowBody(){ setLayout(new GridBagLayout()); JPanel leftPanel = new InformationPanel(); JPanel rightPanel = new GameSelectionPanel(); JSeparator sep = new JSeparator(JSeparator.VERTICAL); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTH; add(leftPanel,gbc); gbc.gridx = 1; gbc.gridy = 0; gbc.fill = GridBagConstraints.VERTICAL; add(sep,gbc); gbc.gridx = 2; gbc.gridy = 0; gbc.fill = GridBagConstraints.NONE; add(rightPanel,gbc); } 

But does JSeperator not show any ideas?

thanks

+7
java user-interface swing gridbaglayout
source share
2 answers

You can try setting the preferred separator width:

 sep.setPreferredSize(new Dimension(5,1)); 

Then make the GridBagLayout use all available height for the separator:

 gbc.fill = GridBagConstraints.VERTICAL; gbc.weighty = 1; 
+11
source share

Adapted from Sun's guide for JSeparator :

In most implementations, the vertical separator has a preferred height of 0, and the horizontal separator has a preferred width of 0. This means that the separator is not displayed unless you either set its preferred size or put it under the control of a layout manager such as BorderLayout or BoxLayout. which stretches it to fill it with an available display area.

The vertical separator has a bit of width (and the horizontal part is height), so you should see some space where the separator is. However, the actual dividing line is not drawn if the width and height are not equal to nonzero.

Maybe you should set the right limits?

+4
source share

All Articles