How to create JSplitPane to divide components in Java?

Java 8Object Oriented ProgrammingProgramming

At first, set two components which we will dividing −

JComponent one = new JLabel("Label One");
one.setBorder(BorderFactory.createLineBorder(Color.yellow));
JComponent two = new JLabel("Label Two");
two.setBorder(BorderFactory.createLineBorder(Color.orange));

Now, split it using SplitPane −

Example

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
public class SwingDemo {
   public static void main(String[] a) {
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JComponent one = new JLabel("Label One");
      one.setBorder(BorderFactory.createLineBorder(Color.yellow));
      JComponent two = new JLabel("Label Two");
      two.setBorder(BorderFactory.createLineBorder(Color.orange));
      JComponent three = new JLabel("Label Three");
      three.setBorder(BorderFactory.createLineBorder(Color.blue));
      JComponent four = new JLabel("Label Four");
      four.setBorder(BorderFactory.createLineBorder(Color.green));
      JSplitPane split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
      split1.setLeftComponent(one);
      split1.setRightComponent(two);
      JSplitPane split2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
      split2.setLeftComponent(three);
      split2.setRightComponent(four);
      frame.add(split1, BorderLayout.NORTH);
      frame.add(split2, BorderLayout.SOUTH);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

Output

raja
Published on 31-May-2019 11:28:31
Advertisements