7 Replies Latest reply on Oct 4, 2006 4:37 AM by camickr

    Regading SplitPanes


      Can anyone please let me know the way to use JSplitPane to accomplsh the following task.

      I have JFrame to hold four panels as four quardants and should be able to resize the panels by dragging the divider of the splitpanes.

      if I drag the horizontal divider down the top-right ,top-left panels should increase in size and bottom-right ,bottom-left panels should decrease in size and viceversa

      if I drag the vertical divider right the top-left ,bottom-left panels should increase in size and top-right ,bottom-right panels should decrease in size and viceversa.

      Can anyone send me the code to acheive this task

      Thanks In Advance

        • 1. Re: Regading SplitPanes
          well, of course
          • 2. Re: Regading SplitPanes
            Hi hpsmartyz

            I couldnt get that response
            Can you send me some example code

            • 3. Re: Regading SplitPanes
              import java.awt.*;
              import java.beans.*;
              import java.awt.event.*;
              import javax.swing.*;
              public class SplitPaneResizing extends JFrame implements PropertyChangeListener
                   JSplitPane left;
                   JSplitPane right;
                   public SplitPaneResizing()
                        JSplitPane main = new JSplitPane();
                        getContentPane().add( main );
                        left = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                        right = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
                        main.setLeftComponent( left );
                        main.setRightComponent( right );
                        left.setTopComponent( createPanel(Color.RED) );
                        left.setBottomComponent( createPanel(Color.GREEN) );
                        right.setTopComponent( createPanel(Color.BLUE) );
                        right.setBottomComponent( createPanel(Color.WHITE) );
                        left.addPropertyChangeListener( this );
                        right.addPropertyChangeListener( this );
                   private JPanel createPanel(Color background)
                        JPanel panel = new JPanel();
                        panel.setPreferredSize( new Dimension(200, 100) );
                        panel.setBackground( background );
                        return panel;
                   public void propertyChange(PropertyChangeEvent e)
                        if ("dividerLocation".equals(e.getPropertyName()))
                             Object source = e.getSource();
                             JSplitPane target = (source.equals(left)) ? right : left;
                             int location = ((Integer)e.getNewValue()).intValue();
                             if (location != target.getDividerLocation())
                                  target.setDividerLocation( location );
                   public static void main(String[] args)
                        SplitPaneResizing frame = new SplitPaneResizing();
                        frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
                        frame.setLocationRelativeTo( null );
              • 4. Re: Regading SplitPanes
                Hi Camickr

                That was great help.
                But can u help me in avoiding teat flickering that happens when I drag the horizontal divider ,which I havent observed while dragging the vertical divider.

                Thank In Advance

                • 5. Re: Regading SplitPanes
                  I don't see any flicker with my example.

                  I have no idea what kind of components you have on your panels (I'm not a mind reader) so the only suggestion is to use the following method on your left/right splitPanes.:


                  • 6. Re: Regading SplitPanes
                    Hi Camickr

                    Sorry , If I have explained the issue in another sense.
                    In SplitPaneResizing.java,we have explicitly made the dividers of the two SpliPanes (left and right) to map each other with respect to any change of the the other.

                    Due to this explict mapping we can find a little variation between the dividers of left and right dividers

                    hope you can help me out.

                    • 7. Re: Regading SplitPanes
                      Addition to my code in reply 3. If you want continuous update of the divider location then you can use: