Package TreeView;
Import java.awt.*;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;
Import Javax.swing.ImageIcon;
Import Javax.swing.JFrame;
Import Javax.swing.JScrollPane;
Import Javax.swing.JTree;
Import Javax.swing.LookAndFeel;
Import Javax.swing.UIManager;
Import javax.swing.UnsupportedLookAndFeelException;
Import Javax.swing.tree.DefaultMutableTreeNode;
Import Javax.swing.tree.DefaultTreeCellRenderer;
Import Javax.swing.tree.DefaultTreeModel;
Import Com.incors.plaf.alloy.AlloyLookAndFeel;
public class Tree {
Public Tree () {
JFrame f = new JFrame ("Treedemo");
Container ContentPane = F.getcontentpane ();
Defaultmutabletreenode root = new Defaultmutabletreenode ("explorer");
Defaultmutabletreenode Node1 = new Defaultmutabletreenode ("My Briefcase");
Defaultmutabletreenode node2 = new Defaultmutabletreenode ("My Computer");
Defaultmutabletreenode node3 = new Defaultmutabletreenode ("Favorites");
Defaultmutabletreenode node4 = new Defaultmutabletreenode ("Readme");
Defaulttreemodel TreeModel = new Defaulttreemodel (root);
Treemodel.insertnodeinto (Node1, Root, Root.getchildcount ());
Treemodel.insertnodeinto (Node2, Root, Root.getchildcount ());
Treemodel.insertnodeinto (Node3, Root, Root.getchildcount ());
Treemodel.insertnodeinto (node4, Root, Root.getchildcount ());
Defaultmutabletreenode Leafnode = new Defaultmutabletreenode ("Company documents");
Treemodel.insertnodeinto (Leafnode, Node1, Node1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("personal letter");
Treemodel.insertnodeinto (Leafnode, Node1, Node1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Private Document");
Treemodel.insertnodeinto (Leafnode, Node1, Node1.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Native disk (C:)");
Treemodel.insertnodeinto (Leafnode, Node2, Node2.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Native disk (D:)");
Treemodel.insertnodeinto (Leafnode, Node2, Node2.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Native disk (E:)");
Treemodel.insertnodeinto (Leafnode, Node2, Node2.getchildcount ());
Defaultmutabletreenode node31 = new Defaultmutabletreenode ("Site list");
Treemodel.insertnodeinto (Node31, Node3, Node3.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Tama Station");
Treemodel.insertnodeinto (Leafnode, Node3, Node3.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Post-bar message");
Treemodel.insertnodeinto (Leafnode, Node3, Node3.getchildcount ());
Leafnode = new Defaultmutabletreenode ("Network Bookstore");
Treemodel.insertnodeinto (Leafnode, Node3, Node3.getchildcount ());
try {
Lookandfeel alloylnf = new Alloylookandfeel ();
Uimanager.setlookandfeel (ALLOYLNF);
} catch (Unsupportedlookandfeelexception ex) {
//Handle the exception here
}
//This line needs To is implemented in order to make JWS properly
Uimanager.getlookandfeeldefaults (). Put ("ClassLoader",
Getcla SS (). getClassLoader ());
JTree tree = new JTree (TreeModel);
Tree.setrowheight (20);
Defaulttreecellrenderer cellrenderer = (defaulttreecellrenderer) tree
. Getcellrenderer ();
Cellrenderer.setleaficon (New ImageIcon ("icons\\117.jpg"));
Cellrenderer.setopenicon (New ImageIcon ("icons\\117.jpg"));
Cellrenderer.setclosedicon (New ImageIcon ("icons\\116.jpg"));
Cellrenderer.setfont (New Font ("Arial", Font.plain, 12));//Set font.
Cellrenderer.setbackgroundnonselectioncolor (Color.White);
Cellrenderer.setbackgroundselectioncolor (Color.yellow);
Cellrenderer.setborderselectioncolor (color.red);
/*
* Change color of text when set or not selected
*/
Cellrenderer.settextnonselectioncolor (Color.Black);
Cellrenderer.settextselectioncolor (Color.Blue);
JScrollPane ScrollPane = new JScrollPane ();
Scrollpane.setviewportview (tree);
Contentpane.add (ScrollPane);
F.pack ();
F.setvisible (TRUE);
F.addwindowlistener (New Windowadapter () {
public void windowclosing (WindowEvent e) {
System.exit (0);
}
});
}
public static void Main (string[] args) {
Tree Tree =new tree ();
}
}
Resources:
from:http://www.iteedu.com/plang/java/jtswingchxshj/57.php
Java JTree Tree Control display