Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSplitPane contents do not resize evenly when window resized with mouse

929204Aug 20 2012
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() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		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.
		
		setVisible(true);
		
		JSplitPane newSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
		newSplitPane.setOneTouchExpandable(true);
		newSplitPane.setBorder(null);
		
		parentContainer.add(newSplitPane);
		
		JPanel panel1 = new JPanel();
		panel1.setBackground(Color.BLUE);
		JPanel panel2 = new JPanel();
		panel2.setBackground(Color.GREEN);
		
		newSplitPane.setTopComponent(panel1);
		newSplitPane.setBottomComponent(panel2);
		
		newSplitPane.setResizeWeight(0.5);
	}
}
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!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 17 2012
Added on Aug 20 2012
0 comments
460 views