JSplitPane Component in Java

This is a tutorial in which we will going to create a program that will have a JSplitPane Component in Java. A JSplitPane is used to let the user divides the two components that can also be resized by the user. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of jSpliPaneComponent.java. 2. Import the following packages:
  1. import java.awt.*;//used to access the Dimension class
  2. import javax.swing.*; //used to access the JFrame, JLabel, JTextField, and JSplitPane component
3. Initialize your variable in your Main, variable frame for JFrame, variable text for JTextField, and variable label for JLabel. The text and label components will then be put inside the JSplitPane.
  1.    JFrame frame = new JFrame("JSplitPane Component");
  2.     JTextArea text = new JTextArea("Sourcecodester is the best!");
  3.     JLabel label = new JLabel("This is Engr. Lyndon Bermoy");
To have the size of the label and text, we will used the setMinimumSize method with the Dimension class having its width and height.
  1.     text.setMinimumSize(new Dimension(200, 200));
  2.     label.setMinimumSize(new Dimension(200, 200));
4. Now, we will use the JSplitPane to divide the label and text into two panes. Have this code below:
  1. JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, text, label);
The JSplitPane above is aligned from left to right using the horizontal alignment of the syntax HORIZONTAL_SPLIT. When we have to align them from top to buttom, we will use the VERTICAL_SPLIT. Take note that the text as JTextField and label as the JLabel was put inside the JSplitPane to divide the components into two. 5. Now, add the JSplitPane variable to the frame using the default BorderLayout of Center position of the getContentPane method. Lastly, set the size, visibility, and the close operation of the frame. Have this code below:
  1.     frame.getContentPane().add(sp, "Center");
  2.    
  3.      frame.setSize(450, 200);
  4.      frame.setVisible(true);
  5.     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
Output: output Here's the full code of this tutorial:
  1. import java.awt.*;//used to access the Dimension class
  2. import javax.swing.*; //used to access the JFrame, JLabel, JTextField, and JSplitPane component
  3.  
  4.  
  5. public class jSplitPaneComponent {
  6.  
  7.  
  8.   public static void main(String args[]) {
  9.    
  10.     JFrame frame = new JFrame("JSplitPane Component");
  11.     JTextArea text = new JTextArea("Sourcecodester is the best!");
  12.     JLabel label = new JLabel("This is Engr. Lyndon Bermoy");
  13.    
  14.     text.setMinimumSize(new Dimension(200, 200));
  15.     label.setMinimumSize(new Dimension(200, 200));
  16.    
  17.     JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, text, label);
  18.    
  19.    
  20.     frame.getContentPane().add(sp, "Center");
  21.    
  22.      frame.setSize(450, 200);
  23.      frame.setVisible(true);
  24.     frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
  25.    
  26.    
  27.   }
  28. }
For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment