Updated March 17, 2023
Introduction to JSplitPane
It is a Java Swing component that helps in dividing two components based on the L&F (Look and Feel) implementation. It also helps in resizing the components even to a minimum size. However, if the minimum size of the components is larger than the JSplitPane, then resizing can’t be done. Two types such as vertical and horizontal splitpane are presently based on the alignment left to right and top to bottom, respectively. Syntax, constructor, and methods of JSplitPane will be discussed in detail in the following sections.
Syntax:
public class JSplitPane extends JComponent implements Accessible
Here, it extends the JComponent class and implements the Accessible interface.
JSplitPane Constructors
It has different constructors with and without arguments. They are mentioned below.
- JSplitPane(): A new split pane is created from left to right( horizontal ) using 2 buttons for components.
- JSplitPane(int orientation): A new split pane is created with orientation specified.
- JSplitPane(int orientation, boolean redraw): A new split pane is created with a specified orientation and redrawing style.
- JSplitPane(int orientation, boolean redraw, Component leftcmp, Component rightcmp): A new split pane is created with specified orientation and redrawing style, left and right component.
- JSplitPane(int orientation, Component leftcmp, Component rightcmp): A new split pane is created with specified orientation, left and right component.
Top 29 Methods of JSplitPane
It offers methods that perform several operations. They are addressed in the below table:
Method | Explanation |
getOrientation() | The orientation of the SplitPane will be returned. |
getUIClassID() | Name of the (Look and Feel) L&F class that renders the component will be returned. |
getUI() | (Look and Feel) L&F object that renders the component will be returned. |
getDividerSize() | Size of the divider will be returned. |
getAccessibleContext() | AccessibleContext linked with the SplitPane will be returned |
setUI(SplitPaneUI u) | (Look and Feel) L&F object that renders the component will be set. |
paramString() | Representation of JSplitPane will be in string format |
getMinimumDividerLocation() | Divider’s smallest or minimum location will be returned. |
remove(Component cmp) | Child component from the splitpane will be removed. |
getDividerLocation() | Last value that is passed to setDividerLocation will be returned. |
isOneTouchExpandable() | Property “oneTouchExpandable” will be returned. |
setRightComponent(Component cmp) | Splitpane’s right component will be set to cmp |
isContinuousLayout() | Property “continuousLayout” will be returned |
getMaximumDividerLocation() | Divider’s largest or maximum location will be returned. |
setLeftComponent(Component cmp) | Splitpane’s left component will be set to cmp |
setBottomComponent(Component cmp) | Splitpane’s bottom component will be set to cmp |
setDividerSize(int s) | Divider’s size will be set. |
setOneTouchExpandable(boolean st) | Property “oneTouchExpandable” will be set. |
setResizeWeight(double w) | When splitpane size changes, this method helps in specifying how the extra space hs to be distributed. |
setLastDividerLocation(int loc)
|
Divider’s last location will be set. |
setTopComponent (Component CMP) | Splitpane’s top component will be set to the parameter CMP. |
setDividerLocation (int loc) | Divider’s location will be set. |
remove(int index) | Component at mentioned index will be removed |
setContinuousLayout (boolean n) | Property “continuous layout” value will be set. In order to continuously redisplay the child components, this value must be true |
getTopComponent ()
|
Splitpane’s top component will be returned using this method. |
setDividerLocation (double loc)
|
Divider’s location will be set as a percentage of the size of the SplitPane. |
getRightComponent () | Splitpane’s right component will be returned. |
getBottomComponent () | Splitpane’s bottom component will be returned. |
addImpl (Component CMP, Object cob, int in) | Mentioned component will be added to the SplitPane. |
Program to Implement JSplitPane
Now, let us see a sample program that demonstrates JSplitPane:
Code:
// Java program to demonstrate JSplitPane
import java.awt.*;
import javax.swing.*;
class JSplitPaneExample extends JFrame
{
//declare the Vertical Pane, Horizontal pane and Panels
private JSplitPane VerticalPane;
private JSplitPane HorizontalPane;
private JPanel P1;
private JPanel P2;
private JPanel P3;
Color cl=new Color(190,190,240);
//constructor of the class
public JSplitPaneExample()
{
setTitle( "Example to demonstrate JSplitPane" );
//setting the background color
setBackground( Color.orange );
//object of the panel
JPanel PanelObj = new JPanel();
//set borderlayout for the panel
PanelObj.setLayout( new BorderLayout() );
getContentPane().add( PanelObj );
// Create three different panels
P1Creation();
P2Creation();
P3Creation();
P1.setBackground(cl);
P2.setBackground(cl);
P3.setBackground(cl);
// Create vertical and horiontal splitter pane
VerticalPane = new JSplitPane( JSplitPane.VERTICAL_SPLIT );
PanelObj.add( VerticalPane, BorderLayout.CENTER );
HorizontalPane = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT );
HorizontalPane.setLeftComponent( P1 );
HorizontalPane.setRightComponent( P2 );
VerticalPane.setLeftComponent( HorizontalPane );
VerticalPane.setRightComponent( P3 );
}
//create panel 1 with 5 buttons
public void P1Creation()
{
P1 = new JPanel();
P1.setLayout( new BorderLayout() );
// Add 5 button
P1.add( new JButton( "N" ), BorderLayout.NORTH );
P1.add( new JButton( "S" ), BorderLayout.SOUTH );
P1.add( new JButton( "E" ), BorderLayout.EAST );
P1.add( new JButton( "W" ), BorderLayout.WEST );
P1.add( new JButton( "C" ), BorderLayout.CENTER );
}
//create panel 2 with 3 buttons
public void P2Creation()
{
P2 = new JPanel();
P2.setLayout( new FlowLayout() );
P2.add( new JButton( "Button A" ) );
P2.add( new JButton( "Button B" ) );
P2.add( new JButton( "Button C" ) );
P2.add( new JButton( "Button D" ) );
P2.add( new JButton( "Button E" ) );
}
//create panel 3 with 1 buttons
public void P3Creation()
{
P3 = new JPanel();
//set borderlayout
P3.setLayout( new BorderLayout() );
//set preferred size
P3.setPreferredSize( new Dimension( 400, 100 ) );
//set the minimum size
P3.setMinimumSize( new Dimension( 100, 50 ) );
P3.add( new JLabel( "Special information: " ), BorderLayout.NORTH );
P3.add( new JTextArea(), BorderLayout.CENTER );
}
public static void main( String args[] )
{
// Create an object of the class
JSplitPaneExample obj = new JSplitPaneExample();
//call pack method that helps the subcomponents of preferred size
obj.pack();
//shows the window
obj.setVisible( true );
}
}
Output:
Here, an output is generated with 3 panels containing different buttons. In Panel1, five buttons N, E, W, S, and C are present whereas, in panel 2, five buttons A, B, C, D, and E are present. In the bottom, a panel for special information is also present.
Conclusion
It is a Swing component in Java that helps in dividing the components based on the Look and Feel (L&F) implementation. Moreover, components can also be resized to a minimum size. In this document, constructors, methods and sample program for JSplitPanel is discussed in detail.
Recommended Articles
This is a guide to JSplitPane. Here we discuss meaning, constructors and top 29 methods with its description along with program to implement JSplitPane. You can also go through our other suggested articles to learn more –