Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6528446

JSplitPane lacks of a method to set up the initial divider location

XMLWordPrintable

    • Cause Known
    • generic
    • generic

      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 =================

            Unassigned Unassigned
            mlapshin Mikhail Lapshin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: