JDK-6528446 : JSplitPane lacks of a method to set up the initial divider location
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 7
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2007-02-26
  • Updated: 2021-07-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Relates :  
Description
There is a similar CR (4101306), which is closed. I am reopeninig this issue because users have real troubles with setting up the initial divider location. For example, there is the problem in NetBeans: http://www.netbeans.org/issues/show_bug.cgi?id=93102

JSplitPane lacks for a method to set up the initial divider location in terms of percents or proportions. There is the setDividerLocation(double) method, but it doesn't work unless the splitpane is shown on the screen.

There is a couple of indirect ways to to set up the initial divider location:
- You can set desirable preferred sizes for the left and right splitpane components, however it doesn't always applicable.
- You can use the HierarchyListener to catch the moment when the splitpane becomes visible and set up desirable sizes.

The both ways are not good enough. I would suggest modifying of the setDividerLocation(double) method in such a way, that it can work even for a splitpane in not-realized state. The method could store the desirable proportionalLocation in a private field and then apply it when the splitpane becomes realized.

Maybe there are other ways to give users the ability to set up the initial divider location.
There is a short test case showing the problem:

============ Source Begin ===============
import javax.swing.*;
import java.awt.*;

public class JSplitPane6528446 {

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                setupUI();
            }
        });
    }

    private static void setupUI() {
        JFrame frame = new JFrame();

        JComponent left = new JPanel();
        left.setPreferredSize(new Dimension(200, 100));

        JComponent right = new JPanel();
        right.setPreferredSize(new Dimension(200, 100));

        JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
        frame.add(sp);

        // Want to set the divider closer to the left side,
        // i.e. the right panel should be larger than the left one.
        //
        // Before a fix for 6528446 you can use setDividerLocation()
        // only after frame.pack() or frame.setVisible(true)
        //
        // After the fix the next line should work.
        sp.setDividerLocation(0.25d);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();

//        sp.setDividerLocation(0.25d); // It always works here

        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
============ Source End =================

Comments
WORK AROUND There is an example code showing how to solve the problem with HierarchyListener. However the solution is not good enough, because as it is written in the javadoc "Hierarchy events are provided for notification purposes ONLY." ======= Source Begin ========== import javax.swing.*; import java.awt.*; import java.awt.event.HierarchyListener; import java.awt.event.HierarchyEvent; public class Workaround6528446 { public static void main(String[] args) throws Exception { SwingUtilities.invokeAndWait(new Runnable() { public void run() { (new Workaround6528446()).setupUI(); } }); } private void setupUI() { JFrame frame = new JFrame(); JComponent left = new JPanel(); left.setPreferredSize(new Dimension(200, 100)); JComponent right = new JPanel(); right.setPreferredSize(new Dimension(200, 100)); final JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right); sp.addHierarchyListener(new HierarchyListener(){ public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { sp.setDividerLocation(0.2); //There we set the initial divider locatino sp.removeHierarchyListener(this); } } }); frame.add(sp); frame.setSize(420, 100); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } ======= Source End ==========
30-07-2007

EVALUATION I wrote two fix versions, implementing two fix ideas. However the both ideas turned out not so good. After some discussion with Alex Potochkin we decided to close the CR as "Will Not Fix". 1. The first fix idea. http://sa.sfbay.sun.com/projects/swing_data/7/6528446.0/ To modify the setDividerLocation(double) method in such a way, that it can work even for a split pane in non-realized state. The method will store the desirable proportionalLocation in a private field and then apply it when the split pane becomes realized. It turned out not so good because JSplitPane gets a new internal state, which cannot be read with the existing API. For example, there is no any possibility to read the stored divider location if the location was set before the showing of the split pane. 2. The second fix idea: http://sa.sfbay.sun.com/projects/swing_data/7/6528446.1/ To add a new API, which will allow to set the initial divider location. It turned out not so good because it requires addition of three new API methods to solve such small problem.
30-07-2007

EVALUATION I would suggest modifying of the setDividerLocation(double) method in such a way, that it can work even for a splitpane in not-realized state. The method could store the desirable proportionalLocation in a private field and then apply it when the splitpane becomes realized.
26-06-2007