How to create splitpanes in java using JSplitPane?

Before going to see how we can create split panes first take a look, what the Split Pane actually is? It is nothing but a light weight Swing Container which separates components graphically. Split pane allows two components to be split inside a container, to split more than one component we need to nest split panes and components. The components which we split can be adjusted interactively by user.

To create split panes in java we can use the JSplitPane class which is inherited by JComponent class. Split panes are of two types Horizontal and Vertical , meaning Horizontal Split Pane divides components horizontally and the divider can be moved horizontally. Similarly Vertical split panes divides components vertically and divider can be moved vertically.

JSplitPane Constructors :

First is simple one as shown below creates a split pane with horizontally.

 
public JSplitPane()

In the next constructor we can specify the orientation whether is is horizontal or vertical
 
public JSplitPane( int orientation )

where orientation will be either HORIZONTAL_SPLIT or VERTICAL_SPLIT

Third one is having extra setting which is used when we change the divider position , the child components should get re sized or not.

 
public JSplitPane( int orientation, int isContinuousLayout ) 

continuous layout property can be turned on or off , if we make it ON , the child components resized continuously when we change the divider position.
 
public JSplitPane( int orientation, Component leftComponent, Component rightComponent )

In above constructor components can also be specified.

In last constructor we can have all the settings i.e. orientation, continuousLayout , left component and right component.

 
public JSplitPane( int orientation , boolean continuousLayout, Component leftComponent, Component rightComponent )


Now lets see simple example , how we can create the split panes.

        JPanel childPanel1 = new JPanel();
        JPanel childPanel2 = new JPanel();
        JLabel childLabel1 = new JLabel("Child Component 1  ");
        JLabel childLabel2 = new JLabel("Child component 2");

        childPanel1.add( childLabel1 );
        childPanel2.add( childLabel2 );
        
        JSplitPane splitPaneExample = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, // Horizontal Split selected
                true, childPanel1, childPanel2); // Continuous Layout Property is true

        getContentPane().add(splitPaneExample);


Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

8 comments:

  1. thank you pranav for your great tutorial!
    I'm new in java and your tutorial helps me a lot

    ReplyDelete
  2. what shud I do ,if I want to have split panes more than 3????

    ReplyDelete
  3. Hi Neha,
    If you want split panes more than 3 , you can check following tutorial
    http://docs.oracle.com/javase/tutorial/uiswing/components/splitpane.html#nesting

    or wait for few days, I will write a tutorial for adding split panes more than 3.

    ReplyDelete
  4. well admin if ull write any tutorial for splitpanes ...then plz xplain for 3 back to back splitpanes...the prev 1 s nt workin for me i thnk.. :/

    ReplyDelete
  5. what do you mean by back to back splitpanes? is it one after another horizontally or vertically?

    ReplyDelete
  6. yes it back one after other vertically like dis... !!!

    ReplyDelete
  7. yes it s one after other vertically like dis..!!!

    ReplyDelete