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



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 −

Updated on: 2019-07-30T22:30:26+05:30

164 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements