In JTree If you want to change the JTree's display mode to use Treecellrender, JTREE that can get different display methods by achieving his implementation makes JTREE's display more colorful, how to use this interface. I briefly introduced the following example, I hope to be able to throw the jade.
In fact, this class is mainly achieved by this method:
Public Component Gettreecellrenderercomponent (JTree Tree, Object Value,
Boolean SELECTED,
Boolean Expanded, Boolean Leaf,
Int row, boolean Hasfocus
This method returns a component control, that is, the display style of the node you want to set, of course, you can inherit a subclass of a JComponent class when implementing, or set a private variable in the class and return. There are many parameters in this method, first of all, JTree is the tree you want to set, the corresponding object, then Value
This is actually a node, and through him, you can get the data of the node, and the corresponding child node parent node. Next, SELECTED indicates how it is displayed if it is selected. Expanded indicates how nodes are displayed for the extended state, then the display mode of the leaf node can be set by this condition. There is also this parameter, for those who do not telescopic, if there is a telescopic, the ROW is changed at any time, so be careful, the last one has a focus, set the display mode when there is a focus. Once this method is implemented, you will set this class with the setcellrende () method. By following an example, you will introduce the use of Treecellrenderer. This example is a friend who makes me write it. I will want him to mind. The number is made up of random Double, but if she is in a range, she and his parent node are displayed in different colors. Below is the code, the byte write may be a bit chaotic.
Import javax.swing. *;
Import java.awt. *;
Import java.awt.event. *;
Import javax.swing.tree.defaultmutabletreenode;
Import javax.swing.tree.defaulttreemodel;
Import javax.swing.tree.treecellrenderer;
Import java.util.random;
Import java.awt.event.mouseadapter;
Import java.util.vector;
Import java.util.hashtable;
Import java.util.enumeration;
Import javax.swing.tree.treepath;
Import java.sql.resultset;
Public class gb extends jframe {
PRIVATE TREEMENU Tree;
Public GB () {
Tree = new treemenu ();
this.setsize (500, 550);
this.init ();
THIS.SETDEFAULTCLOSEOPERATION (JFrame.exit_ON_CLOSE);
THIS.SHOW ();
}
Private void init ()
{
Container cp = this.getContentPane ();
Cp.setLayout (null);
JScrollpane JSP = New Jscrollpane (Tree);
Jsp.setbounds (100, 20, 300, 500);
Cp.Add (JSP);
Public static void main (String [] args) {
GB GB = New GB ();
}
}
Class TreeMenu Extends Jtree
{
Private defaultmutabletreenode root;
Private defaulttreemodel model;
Public treemenu ()
{
Super ();
THIS.SETROOTVISIBLE (TRUE);
NodeData Rootdata = New NodeData ("root");
Root = new defaultmutabletreenode (rootdata);
Model = New DefaultTreeModel (root);
Model.setroot (root);
This.SetModel (Model);
This.Addmouselistener (New LinkToevent ());
This.SetCellrenderer (New Cellrender ());
THIS.TESTLOADDATA ();
}
Private void testloaddata ()
{
Hashtable table = new hashtable ();
Long seeds = java.util.calendar.getInstance (). gettimeinmillis ();
For (int i = 0; i <10; i )
{
Random R = New Random (SEEDS);
NodeData nd = new nodedata (String.Valueof (R.Nextint (1000)));
Seeds = r.nextint (1000);
Vector vec = new vector ();
For (int J = 0; j <3; j )
{
Double min = r.nextdouble ();
SEEDS = min;
Double Max = r.nextdouble ();
SEEDS = max;
Double value = r.nextdouble ();
SEEDS = VALUE;
Vec.AddeElement (New Nodedata (Min, Max, Value, "www.9cbs.net");
}
Table.put (ND, VEC);
}
This.loadData (TABLE);
}
Public void loadingData (havehtable nodes)
{
Vector Child;
Vector parent = new vector ();
ENUMERATION ENUM = NODES.KEYS ();
While (enum.hasmoreElements ())
{
Parent.addelement (enum.nexTelement ());
}
For (Int i = parent.size () - 1; i> 0; i -)
{
Child = (Vector) Nodes.get (Parent.ementat (i));
DefaultmutableTreenode Temp = this.addparentNode
(NodeData) Parent.ementat (i));
DefaultMutableTreenode ParentNode =
this.addparentNode (Temp, New Nodedata ("Three")); for (int J = 0; j { THIS.AddchildNode (ParentNode, (NodeData) child.ementat (j)); } } } Public DefaultmutableTreenode AddparentNode (NodeData NodeData) { DefaultMutableTreenode Newode = New DefaultMutableTreenode (NodeData); (DefaultmutableTreenode) Model.Getroot ()). Add (newnode); Return newNode; } Public defaultmutabletreenode addparentNode (DefaultmutableTreenode ParentNode, NodeData NodeData) { DefaultMutableTreenode Newode = New DefaultMutableTreenode (NodeData); ParentNode.Add (NewNode); Return newNode; } Public void addChildNode (Defaultmutabletreenode Node, NodeData NodeData) { DEFAULTMUTABLETREENODE ChildNode = New DefaultmutableTreenode (NodeData); node.add (childnode); } Public void rebuild () { Root.removeAllChildren (); THIS.TESTLOADDATA (); } Class LinkToevent Extends MouseAdapter { Public void mouseclicked (MouseEvent Me) { Defaultmutabletreenode Node; TreePath SELECTEDPATH = TreeMenu.This.getPathforLocation (me.getx (), me.gety ()); IF (SELECTEDPATH! = NULL) { Node = (Defaultmutabletreenode) SELECTEDPATH.GetlastPathComponent (); IF (Node! = null && node.isleaf ()) { NodeData Nd = (NodeData) node.getuserObject (); // The Leaf That HAS URL WILL BE Display A Dialog. IF (nd.isleaf ()) { JOPTIONPANE.SHOWCONFIRMDIALOG (TreeMenu.This, nd.geturl ()); // You can Implment your function. } } } } } } Class NodeData { PRIVATE STRING NAME; Private double min; Private Double Max; Private Double value; Private boolean isleaf = false; Private string URL = NULL; Public NodeData (String Name) { THIS.NAME = Name; THIS.IsLEAF = FALSE; } Public NodeData (Double Min, Double Max, Double Value, String URL) { THIS.MIN = min; THIS.MAX = Max; THIS.VALUE = VALUE; THIS.IsLEAF = TRUE; this.url = URL; } Public boolean isleaf () { Return this.isleaf; } Public boolean isoverflow () { IF (this.isleaf == True) { IF (value <= max && value> = min) { Return True; } Else { Return False; } } Else { Return False; } } Public String Tostring () { IF (this.isleaf ()) { Return String.Valueof (Value); } Else { Return this.name; } } Public void seturl (String newURL) { THIS.URL = NewURL; } Public String getURL () { Return this.ur; } } Class Cellrender Extends Jlabel Implements treecellrenderer { Protected color selectedback; Protected color selected process; protected color noselectedback; Protected color noselected process; Protected color overflowbackground = color.yellow; Protected color overflowforeforeground = color.red; protected color overflowselectedbg = color.green; protected color overflowselectedfg = color.black; Public Cellrender () { Super (); This.SelectedbackGround = color.blue; This.selectedForeground = Color.green; THIS.NOSELECTEDBACKGROUND = Color.White; THIS.NOSELECTEDFOREGROUND = color.blue; This.SetForeground (this.noselectedForeground); This.SetBackground (this.noselectedBackground); } Public Component Gettreecellrenderercomponent (JTree Tree, Object Value, Boolean SELECTED, Boolean Expanded, Boolean Leaf, INT Row, Boolean Hasfocus) { Defaultmutabletreenode node = (defaultmutabletreenode) value; NodeData ND; IF (Leaf) { Nd = (nodedata) node.getuserObject (); } Else { Nd = checkchild (node); IF (! nd.isoverflow ()) { IF (SELECTED) { This.SetForeground (this.SelectedForeground); THIS.SETBACKGROUND (this.selectedback } Else { This.SetBackground (this.noselectedBackground); This.SetForeground (this.noselectedForeground); } } Else { IF (SELECTED) { This.SetBackground (this.overflowSelectedBG); This.SetForeground (this.overflowSelectedfg); } Else { This.SetBackground (this.overflowback); This.SetForeground (this.OverflowForeground); } } THIS.SETTEXT (VALUE.TOSTRING ()); Return this; } Private NodeData Checkchild (DefaultmutableTreenode ChildNode) { NodeData Child = NULL; INT count = childNode.getChildcount (); For (int i = 0; i { DefaultMutableTreenode ChildNodes = (Defaultmutabletreenode) ChildNode.getChildat (i); if (ChildNodes.Isleaf ()) { ChildNodes.getuserObject (); if (Child.Isoverflow ()) { Break; } } Else { Child = Checkchild (childnodes); } } Return Child; } }