javauser-interfaceswinggridbaglayout

JSeparator wont show with 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 the JSeperator doesn't show, any ideas?

Thanks


Solution

  • You could try to set the preferred width for the separator:

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

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

    gbc.fill = GridBagConstraints.VERTICAL;
    gbc.weighty = 1;