How to create a nested JSplitPane in Java?

Java 8Object Oriented ProgrammingProgramming

Let us first create three components −

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));

Now, we will split one and two components.

JSplitPane split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two);
split1.setDividerLocation(0.5);
split1.setDividerSize(2);

After that the above splitted pane would be splitted with component three making the process nested −

JSplitPane split2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,split1, three );
split2.setDividerLocation(0.9);
split2.setDividerSize(2);

The following is an example to create nested JSplitPane −

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));
      JSplitPane split1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, one, two);
      split1.setDividerLocation(0.5);
      split1.setDividerSize(2);
      JSplitPane split2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,split1, three );
      split2.setDividerLocation(0.9);
      split2.setDividerSize(2);
      frame.add(split2,BorderLayout.CENTER);
      frame.setSize(550, 250);
      frame.setVisible(true);
   }
}

Output

raja
Published on 04-Jun-2019 07:39:58
Advertisements