How to set orientation and split components along x-axis in Java?

Java 8Object Oriented ProgrammingProgramming

Let us first create components to split. Here. We have two labels −

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

Now, set orientation and split along x-axis with HORIZONTAL_SPLIT −

JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
split.setLeftComponent(one);
split.setRightComponent(two);

The following is an example to set orientation and split components along x-axis in Java −

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.red));
      JComponent two = new JLabel("Label Two");
      two.setBorder(BorderFactory.createLineBorder(Color.blue));
      JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
      split.setLeftComponent(one);
      split.setRightComponent(two);
      frame.add(split, BorderLayout.NORTH);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

This will produce the following output −

raja
Published on 03-Jun-2019 12:12:01
Advertisements