Forum Stats

  • 3,728,681 Users
  • 2,245,675 Discussions
  • 7,853,700 Comments

Discussions

Regading SplitPanes

843804
843804 Member Posts: 45,751
edited October 2006 in Swing
Hi

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
Sharmila




Comments

  • 843804
    843804 Member Posts: 45,751
    well, of course
  • 843804
    843804 Member Posts: 45,751
    Hi hpsmartyz

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

    Regards
    Sharmila
  • camickr
    camickr Member Posts: 24,931
    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.pack();
    		frame.setLocationRelativeTo( null );
    		frame.setVisible(true);
    	}
    }
  • 843804
    843804 Member Posts: 45,751
    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
    Sharmila

  • camickr
    camickr Member Posts: 24,931
    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.:

    setContinuousLayout(false);


  • 843804
    843804 Member Posts: 45,751
    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.

    Regards
    Sharmila
  • camickr
    camickr Member Posts: 24,931
    Addition to my code in reply 3. If you want continuous update of the divider location then you can use:
    left.setContinuousLayout(true);
    right.setContinuousLayout(true);
This discussion has been closed.