JSeparator wont show with GridBagLayout

前端 未结 2 927
北恋
北恋 2021-01-12 02:51

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

public MainWindowBody(){
    setLayout(new G         


        
相关标签:
2条回答
  • 2021-01-12 03:03

    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;
    
    0 讨论(0)
  • 2021-01-12 03:13

    Taken from Sun's guide for JSeparator:

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

    The vertical separator does have a bit of width (and the horizontal a bit of height), so you should see some space where the separator is. However, the actual dividing line isn't drawn unless the width and height are both non-zero.

    Maybe you should set right constraints?

    0 讨论(0)
提交回复
热议问题