Simple Tree

import java.awt.BorderLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

public class Tree2 extends JFrame {
	JTree tree;

	DefaultTreeModel treeModel;

	public Tree2() {
		super("Tree Test Example");
		setSize(200, 150);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		UIManager.put("Tree.leafIcon", new ImageIcon("leafIcon.gif"));
		UIManager.put("Tree.openIcon", new ImageIcon("openIcon.gif"));
		UIManager.put("Tree.closedIcon", new ImageIcon("closeIcon.gif"));
		UIManager.put("Tree.expandedIcon", new ImageIcon("expandedIcon.gif"));
		UIManager.put("Tree.collapsedIcon", new ImageIcon("collapsedIcon.gif"));

	}

	public void init() {
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
		DefaultMutableTreeNode subroot = new DefaultMutableTreeNode("SubRoot");
		DefaultMutableTreeNode leaf1 = new DefaultMutableTreeNode("Leaf 1");
		DefaultMutableTreeNode leaf2 = new DefaultMutableTreeNode("Leaf 2");

		treeModel = new DefaultTreeModel(root);
		tree = new JTree(treeModel);

		treeModel.insertNodeInto(subroot, root, 0);
		subroot.add(leaf1);
		root.add(leaf2);

		// tree.putClientProperty("JTree.lineStyle", "Angled");
		getContentPane().add(tree, BorderLayout.CENTER);
	}

	public static void main(String args[]) {
		Tree2 tt = new Tree2();
		tt.init();
		tt.setVisible(true);
	}
}

Download

Tree in Swing

Simple Tree


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code