Java Swing | JSplitPane with Examples
JSplitPane is a part of Java Swing. JSplitPane is used to divide only two components. JSplitPane is to use resize the components . By using the JSplitPane the user can manually resize the component till its minimum size . JSplitPane can be of two types, one is the vertical and horizontal splitpane
Constructor of JSplitPane are:
- JSplitPane() : creates a new split pane that is oriented horizontally
- JSplitPane(int o) : creates a new split pane with orientation mentioned.
- JSplitPane(int o, boolean r) : creates a new split pane with orientation and redrawing style mentioned.
- JSplitPane(int o, boolean r, Component l, Component r) : creates a new split pane with orientation, redrawing style and left and right component mentioned.
- JSplitPane(int o, Component l, Component r): creates a new split pane with orientation and left and right component mentioned.
Commonly used Functions of JSplitPane are :
- getOrientation() : returns the orientation of the split pane
- getRightComponent() : returns the right component of the splitpane
- getTopComponent() : returns the top component of the splitpane
- isContinuousLayout() :returns the continuous layout property
- getBottomComponent() : returns the Bottom component of the splitpane
- setRightComponent(Component c) : the right component of the splitpane is set to c
- setLeftComponent(Component c) : the left component of the splitpane is set to c
- setTopComponent(Component c) : the top component of the splitpane is set to c
- setBottomComponent(Component c) : the bottom component of the splitpane is set to c
- setUI(SplitPaneUI ui) : Sets the Look and feel object that renders this component.
- setResizeWeight(double v) :Specifies how to distribute extra space when the size of the split pane changes.
- setOneTouchExpandable(boolean n) : Sets the value of the oneTouchExpandable property, whichwhen true provides a UI widget that can collapse or expand the components on click
- setDividerLocation(int l) : Sets the location of the divider.
- setDividerSize(int n) : Sets the size of the divider.
- setLastDividerLocation(int n) : Sets the last location the divider.
- setDividerLocation(double p) : Sets the divider location as a percentage of the JSplitPane’s size.
- setContinuousLayout(boolean n) : Sets the value of the continuousLayout property, which must be true for the child components to be continuously redisplayed .
- remove(int index) : removes the Component at the specified index.
- remove(Component c) : Removes the child component from the pane.
- isOneTouchExpandable() : returns the oneTouchExpandable property.
- isContinuousLayout() : returns the continuousLayout property.
- getMinimumDividerLocation() : returns the minimum location of the divider .
- getMaximumDividerLocation() : Returns the maximum location of the divider .
- getDividerSize() : Returns the size of the divider.
- getDividerLocation() : returns the last value passed to setDividerLocation.
- addImpl(Component c, Object co, int i) : adds the specified component to this split pane.
- setUI(SplitPaneUI ui) : sets the look and feel object that renders this component.
- getUI() : returns the look and feel object that renders this component.
- paramString() : returns a string representation of this JSplitPane.
- getUIClassID() : returns the name of the Look and feel class that renders this component.
- getAccessibleContext() : gets the AccessibleContext associated with this JSplitPane.
The Following programs will illustrate the use of JSplitPane
1. Program to create a horizontal JSplitPane to separate two text areas
// Java Program to create a horizontal JSplitPane // to separate two text areas import javax.swing.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame { // frame static JFrame f; // text areas static JTextArea t1, t2; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object solve s = new solve(); // create a panel JPanel p1 = new JPanel(); JPanel p = new JPanel(); // create text areas t1 = new JTextArea( 10 , 10 ); t2 = new JTextArea( 10 , 10 ); // set texts t1.setText( "this is first text area" ); t2.setText( "this is second text area" ); // add text area to panel p1.add(t1); p.add(t2); // create a splitpane JSplitPane sl = new JSplitPane(SwingConstants.HORIZONTAL, p1, p); // add panel f.add(sl); // set the size of frame f.setSize( 300 , 300 ); f.show(); } } |
Output :
2.Program to create a Vertical JSplitPane to separate two text areas
// Java Program to create a vertical // JSplitPane to separate two text areas import javax.swing.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame { // frame static JFrame f; // text areas static JTextArea t1, t2; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object solve s = new solve(); // create a panel JPanel p1 = new JPanel(); JPanel p = new JPanel(); // create text areas t1 = new JTextArea( 10 , 10 ); t2 = new JTextArea( 10 , 10 ); // set texts t1.setText( "this is first text area" ); t2.setText( "this is second text area" ); // add text area to panel p1.add(t1); p.add(t2); // create a splitpane JSplitPane sl = new JSplitPane(SwingConstants.VERTICAL, p1, p); // set Orientation for slider sl.setOrientation(SwingConstants.VERTICAL); // add panel f.add(sl); // set the size of frame f.setSize( 300 , 300 ); f.show(); } } |
Output :
2. Java Program to create nested JSplitPane, one of them is one Touch Expandable
// Java Program to create nested JSplitPane, // one of them is one Touch Expandable import javax.swing.event.*; import java.awt.*; import javax.swing.*; class solve extends JFrame { // frame static JFrame f; // text areas static JTextArea t1, t2, t3, t4; // main class public static void main(String[] args) { // create a new frame f = new JFrame( "frame" ); // create a object solve s = new solve(); // create a panel JPanel p1 = new JPanel(); JPanel p = new JPanel(); JPanel p2 = new JPanel(); JPanel p3 = new JPanel(); // create text areas t1 = new JTextArea( 10 , 10 ); t2 = new JTextArea( 10 , 10 ); t3 = new JTextArea( 10 , 10 ); t4 = new JTextArea( 10 , 10 ); // set texts t1.setText( "this is first text area" ); t2.setText( "this is second text area" ); t3.setText( "this is third text area" ); t4.setText( "this is fourth text area" ); // add text area to panel p1.add(t1); p.add(t2); p2.add(t3); p3.add(t4); // create a splitpane JSplitPane sl = new JSplitPane(SwingConstants.VERTICAL, p1, p); JSplitPane s2 = new JSplitPane(SwingConstants.VERTICAL, p2, p3); // set Orientation for slider sl.setOrientation(SwingConstants.VERTICAL); s2.setOrientation(SwingConstants.VERTICAL); s2.setOneTouchExpandable( true ); // set divider location sl.setDividerLocation( 70 ); // set Layout for frame f.setLayout( new FlowLayout()); // add panel f.add(sl); f.add(s2); // set the size of frame f.setSize( 600 , 300 ); f.show(); } } |
Output:
Note : This above programs might not run in an online compiler please use an offline IDE
Please Login to comment...