Java JTree Tree Control display

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.