0 Replies Latest reply: Aug 21, 2012 3:53 AM by 929204 RSS

    JSplitPane contents do not resize evenly when window resized with mouse

      Hello everyone,

      Despite the fact that I've called "setResizeWeight(0.5)", the two JPanels I put in my JSplitPane do not resize evenly (that is, the divider does not stay in the exact center of the JSplitPane) when the window is resized with the mouse by clicking on a window edge and dragging.

      Here is the code:
      class SplitPaneTest extends JFrame {
           public SplitPaneTest() {
                Container winContent = getContentPane();
                winContent.setLayout(new BorderLayout());
                JPanel parentContainer = new JPanel();
                parentContainer.setLayout(new GridLayout());
                winContent.add(parentContainer, BorderLayout.CENTER);
                setSize(500, 500);
                setLocationRelativeTo(null); // "null" centers the window relative to the primary monitor.
                JSplitPane newSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                JPanel panel1 = new JPanel();
                JPanel panel2 = new JPanel();
      When the above class is instantiated on the EDT, it creates the window containing the vertically-split JSplitPane, and the divider is neatly placed in the very center of the window. No problem there.
      So, I click on the bottom edge of the window with the mouse and slowly drag up, reducing the height of the window. As the window gets smaller, it's plain to see that the window is resizing such that the top panel remains larger than the bottom panel. The divider does move towards the top of the window as I drag the mouse up, but it doesn't stay in the dead center of the window.

      Why is this happening, and how can I ensure that the divider stays in the center as the window is resized?
      Thanks in advance!