Do you think IIT Guwahati certified course can help you in your career?
No
Introduction 📚
Java is an open-source, high-level, object-oriented programming language. It is used for desktop applications, web applications, database connectivity, game development, and more. It is also a fast, reliable, robust, platform-independent, dynamic, interpreted, and secure🔒 programming language.
This blog will teach us about JSplitPane In Java, its constructors, and methods.
What Is JSplitPane❓
JSplitPane in Java is a part of java swing. It is used for dividing two and only two components which the user can then resize until the minimum size is reached. Vertical JSplitPane (used for aligning the components from top to bottom) and Horizontal JSplitPane (used for aligning the components from left to right) are two types of JSplitPanes available.
Creates a new JSplitPane with the specified orientation and the specified components.
Fields of JSplitPane
Following are some of the fields in JSplitpane are-
static String BOTTOM:This field is used to add one component below another.
static String CONTINUOUS_LAYOUT_PROPERTY:We use this field for bounding property name for componentLayout.
static String DIVIDER:This field is used for adding a component representing the divider component.
static int HORIZONTAL_SPLIT: This field indicates that the components are to be divided along the x-axis.
static int VERTICAL_SPLIT:For dividing the components along the y-axis Vertical_split field is used.
protected Component LEFT:Like the bottom field, the left-field is used to add a component to the left of another component.
protected Component RIGHT:For adding one component to the right of some other component, we use the RIGHT field.
Methods of JSplitPane 📝
After learning about the types of constructors and common field used in JSplitPane, we will now learn about methods commonly used in JSplitPane in java.
protected void addImpl(Component comp, Object constraints, int index):We use this method to add a component to the container at a specified index. Also, for adding the specified component to this container’s layout using the specified constraints, object LayoutManager is notified. For this addLayoutComponent method is used.
public void removes (Component comp):This method removes a child component from the pane.
public void removes (int index):We use this method to remove a component from the pane's specified index.
public void removeAll():This method removes all the child components from the JSplitPane.
public boolean getMaximumDividerLocation(): This method returns the divider's maximum location from the look and feels implementation.
public boolean getMinimumDividerLocation():This method returns the minimum location of the divider from the look and feels implementation.
void setOrientation(int orientation): This method is used to set the orientation or how the splitter will divide the pane.
protected String paramString():This method returns a string representation of JSplitPane.
SplitPaneUI getUI(Component c):The method returns the split pane UI providing the current look and feel.
Example 👩🏻🏫
For this example, we have used Eclipse. You can use any IDE of your choice.😁
Open your IDE and copy-paste the following code into the java file (.java) you created like this. For example, we have written the code in the 'main.java' file. Here we have to implement a vertical split component.
Code
/*
Java Program for vertical JSplitPane
*/
import javax.swing.event.*;
import java.awt.*;
import javax.swing.*;
public class Main extends JFrame {
// Creating frame
static JFrame f;
// Creating two text areas
static JTextArea t1, t2;
// Driver code
public static void main(String[] args)
{
// Create a new frame
f = new JFrame("JSplitPane Example");
// Create a object
Main s = new Main();
// Create a panel
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
// Create text areas
t1 = new JTextArea(20, 20);
t2 = new JTextArea(20, 20);
// Add texts
t1.setText("Coding Ninja");
t2.setText("is the best");
// Set color of text areas
t1.setBackground(Color.GREEN);
t2.setBackground(Color.ORANGE);
// Add text area to panel
p1.add(t1);
p2.add(t2);
// Create vertical JSplitPane
JSplitPane sl = new JSplitPane(SwingConstants.VERTICAL, p1, p2);
// Setting Orientation
sl.setOrientation(SwingConstants.VERTICAL);
// Adding panel to frame
f.add(sl);
// Set the size of frame
f.setSize(300, 300);
f.show();
}
}
You can also try this code with Online Java Compiler
The above code shows how to split two components in the vertical orientation.
Firstly we have created a frame and two text areas, ' t1' and 't2'.
In the main function, we have instantiated the frame to create a new frame window.
Then we created two panels, 'p1' and 'p2', followed by setting the size of text areas and adding text in both.
To make the window look colorful, we have changed the background color of textareas using thesetBackground() function.
Then we added these fully created text areas into their respective panels and finally added both these panels in JSplitPane, providing vertical orientation.
Lastly, this whole JSplitPane is added to the above-created frame.
Output
Vertical orientation :
Similarly, correspondingly horizontal orientation is:
Java is a class-based, high-level, object-oriented programming language with as many implementation dependencies as possible.
What is JSplitPane In Java?
JSplitPane in Java is a part of java swing. It is used for dividing two and only two components which the user can then resize until the minimum size is reached.
Mention class hierarchy for JSplitPane in Java?
The class hierarchy is as follows = java.lang.Object -> java.awt.Component -> java.awt.Container -> javax.swing.JComponent -> javax.swing.JSplitPane
What is the swing package in java?
Swing packages in java provide classes of java swing APIs, for example, JLayeredPane, JRadioButton, JMenu, JColorChooser, JButton, JTextField, JLayeredPane, etc.
JSplitPane in Java is included in which package?
JSplitPane is a part of the Java Swing package.
Conclusion 🙋🏻♀️
In this article, we have covered JSplitPane in Java. We have also discussed its class declaration, constructors, layers, and a few important methods of JSplitPane In Java, along with an example.
if you would like to learn more, check out our articles on-