JSplitPane splitting 50% precisely

后端 未结 4 923
悲&欢浪女
悲&欢浪女 2021-02-03 23:24

In Swing, what\'s the best way to make the JSplitPane to split two jpanels with 50% size each.

It looks like if I don\'t set preferred sizes on the panels it always make

4条回答
  •  有刺的猬
    2021-02-04 00:02

    I had a similar problem and I solved it by using a component listener in the parent container and set the divider location on the first resize. Initialise a variable firstResize to true and add this into the parent container constructor:

    addComponentListener(new ComponentAdapter(){
        @Override
        public void componentResized(ComponentEvent e) {
            if(firstResize){
                splitPane.setDividerLocation(0.5);
                firstResize = false;
            }
        }
    });
    

    This should cause the divider to be centred when the parent container size is first set.

提交回复
热议问题