Adding Line to JTree

In this section, you will learn to create JTree along with
a dividing Line. Here the tree gets divided into two parts separated by a line.
Both trees has the same root and the nodes. A JScrollPane
provides a Scrollable property for viewing components to scroll.
Program Description:
This program creates a box and two trees (tree1 and
tree2). These trees contains the multiple nodes with a root and scrollable property
to scroll on the scroll bar to view nodes. The box divides frame into to two parts
and sets the requirements. Here the first tree sets in the west side on the frame
and another on the east side.
Here is the code of program:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Container;
public class JTreeLineExample{
public static void main(String args[]) {
JFrame fm = new JFrame("JTree Line frame");
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container ca = fm.getContentPane();
Box box = Box.createHorizontalBox();
JTree tree1 = new JTree();
JScrollPane scroll1 = new JScrollPane(tree1);
JTree tree2 = new JTree();
JScrollPane scroll2 = new JScrollPane(tree2);
box.add(scroll1, BorderLayout.WEST);
box.add(scroll2, BorderLayout.EAST);
fm.getContentPane().add(box, BorderLayout.CENTER);
fm.setSize(300, 250);
fm.setVisible(true);
}
}
|
Download this program.
Output this program:


|