Checknode. Java
Import java. util. enumeration;
Import javax. Swing. Tree. defaultmutabletreenode;
/**
* Node of the chceknodetree tree
* @ Version 1.1 01/15/99
*/
Public class checknode extends defaultmutabletreenode {
Public final static int single_selection = 0;
Public final static int dig_in_selection = 4;
Protected int selectionmode;
Protected Boolean isselected;
Public checknode (){
This (null );
}
Public checknode (Object userobject ){
This (userobject, true, false );
}
Public checknode (Object userobject, Boolean allowschildren,
Boolean isselected ){
Super (userobject, allowschil.pdf );
This. isselected = isselected;
Setselectionmode (dig_in_selection );
}
Public void setselectionmode (INT mode ){
Selectionmode = mode;
}
Public int getselectionmode (){
Return selectionmode;
}
Public void setselected (Boolean isselected ){
This. isselected = isselected;
If (selectionmode = dig_in_selection) & (children! = NULL )){
Enumeration E = children. Elements ();
While (E. hasmoreelements ()){
Checknode node = (checknode) E. nextelement ();
Node. setselected (isselected );
}
}
}
Public Boolean isselected (){
Return isselected;
}
// If you want to change "isselected" by celleditor,
/*
Public void setuserobject (Object OBJ) {If (OBJ instanceof Boolean ){
* Setselected (Boolean) OBJ). booleanvalue ();} else {
* Super. setuserobject (OBJ );}}
*/
}
Checknodetree. Java
Import javax. Swing. Icon;
Import javax. Swing. jcheckbox;
Import javax. Swing. jframe;
Import javax. Swing. jlabel;
Import javax. Swing. jpanel;
Import javax. Swing. jscrollpane;
Import javax. Swing. jtree;
Import javax. Swing. uimanager;
Import javax. Swing. plaf. coloruiresource;
Import javax. Swing. Tree. defaultmutabletreenode;
Import javax. Swing. Tree. defaulttreecellrenderer;
Import javax. Swing. Tree. defaulttreemodel;
Import javax. Swing. Tree. treecellrenderer;
Import javax. Swing. Tree. treepath;
Import javax. Swing. Tree. treeselectionmodel;
Import javax. Swing. Tree. treemodel;
/**
* Jtree example with chcek
* @ Version 1.1 01/15/99
*/
Public class checknodetree extends jtree {
Public checknodetree (){
This (New defaulttreemodel (null ));
}
Public checknodetree (treemodel model ){
Super (model );
Setcellrenderer (New checkrenderer ());
Getselectionmodel (). setselectionmode (
Treeselectionmodel. single_tree_selection
);
Addmouselistener (New nodeselectionlistener (this ));
}
Class nodeselectionlistener extends mouseadapter {
Jtree tree;
Nodeselectionlistener (jtree tree ){
This. Tree = tree;
}
Public void mouseclicked (mouseevent e ){
Int x = E. getx ();
Int y = E. Gety ();
Int ROW = tree. getrowforlocation (x, y );
Treepath Path = tree. getpathforrow (ROW );
If (path! = NULL ){
Defaultmutabletreenode node = (defaultmutabletreenode) path. getlastpathcomponent ();
If (node instanceof checknode ){
Checknode chnode = (checknode) node;
Boolean isselected =! (Chnode. isselected ());
Chnode. setselected (isselected );
If (chnode. getselectionmode () = checknode. dig_in_selection ){
If (isselected ){
Tree. expandpath (PATH );
} Else {
Tree. collapsepath (PATH );
}
}
(Defatretreemodel) tree. GetModel (). nodechanged (node );
// I need revalidate If node is root. But why?
If (ROW = 0 ){
Tree. revalidate ();
Tree. repaint ();
}
}
}
}
}
Public static void main (string ARGs []) {
String [] STRs = {"Swing", // 0
"Platf", // 1
"Basic", // 2
"Metal", // 3
"Jtree"}; // 4
Checknode [] nodes = new checknode [STRs. Length];
Defaultmutabletreenode root = new defaultmutabletreenode ("root ");
For (INT I = 0; I <STRs. length; I ++ ){
Nodes [I] = new checknode (STRs [I]);
}
Root. Add (nodes [0]);
Nodes [0]. Add (nodes [1]);
Nodes [1]. Add (nodes [2]);
Nodes [1]. Add (nodes [3]);
Nodes [0]. Add (nodes [4]);
Defaulttreemodel model = new defaulttreemodel (Root );
Checknodetree tree = new checknodetree (model );
Jframe frame = new jframe ("test ");
Frame. addwindowlistener (New windowadapter (){
Public void windowclosing (windowevent e ){
System. Exit (0 );
}
});
Jscrollpane JSP = new jscrollpane (tree );
Frame. getcontentpane (). setlayout (New borderlayout ());
Frame. getcontentpane (). Add (JSP, borderlayout. center );
Frame. setsize (300,200 );
Frame. setvisible (true );
}
}
Class checkrenderer extends jpanel implements treecellrenderer {
Protected jcheckbox check;
Protected treelabel label;
Defaulttreecellrenderer dtcr = new defaulttreecellrenderer ();
Public checkrenderer (){
Setlayout (null );
Add (check = new jcheckbox ());
Add (Label = new treelabel ());
Check. setbackground (uimanager. getcolor ("tree. textbackground "));
Label. setforeground (uimanager. getcolor ("tree. textforeground "));
}
Public component gettreecellrenderercomponent (jtree tree, object value,
Boolean isselected, Boolean expanded, Boolean leaf, int row,
Boolean hasfocus ){
If (! (Value instanceof checknode )){
Return dtcr. gettreecellrenderercomponent (tree, value, isselected, expanded, leaf, row, hasfocus );
}
String stringvalue = tree. convertvaluetotext (value, isselected,
Expanded, leaf, row, hasfocus );
Setenabled (tree. isenabled ());
Check. setselected (checknode) value). isselected ());
Label. setfont (tree. getfont ());
Label. settext (stringvalue );
Label. setselected (isselected );
Label. setfocus (hasfocus );
If (leaf ){
Label. seticon (uimanager. getIcon ("tree. leaficon "));
} Else if (expanded ){
Label. seticon (uimanager. getIcon ("tree. openicon "));
} Else {
Label. seticon (uimanager. getIcon ("tree. closedicon "));
}
Return this;
}
Public dimension getpreferredsize (){
Dimension d_check = check. getpreferredsize ();
Dimension d_label = label. getpreferredsize ();
Return new dimension (d_check.width + d_label.width,
(D_check.height <d_label.height? D_label.height
: D_check.height ));
}
Public void dolayout (){
Dimension d_check = check. getpreferredsize ();
Dimension d_label = label. getpreferredsize ();
Int y_check = 0;
Int y_label = 0;
If (d_check.height <d_label.height ){
Y_check = (d_label.height-d_check.height)/2;
} Else {
Y_label = (d_check.height-d_label.height)/2;
}
Check. setlocation (0, y_check );
Check. setbounds (0, y_check, d_check.width, d_check.height );
Label. setlocation (d_check.width, y_label );
Label. setbounds (d_check.width, y_label, d_label.width, d_label.height );
}
Public void setbackground (color ){
If (color instanceof coloruiresource)
Color = NULL;
Super. setbackground (color );
}
Public class treelabel extends jlabel {
Boolean isselected;
Boolean hasfocus;
Public treelabel (){
}
Public void setbackground (color ){
If (color instanceof coloruiresource)
Color = NULL;
Super. setbackground (color );
}
Public void paint (Graphics g ){
String STR;
If (STR = gettext ())! = NULL ){
If (0 <Str. Length ()){
If (isselected ){
G. setcolor (uimanager
. Getcolor ("tree. selectionbackground "));
} Else {
G. setcolor (uimanager. getcolor ("tree. textbackground "));
}
Dimension D = getpreferredsize ();
Int imageoffset = 0;
Icon currenti = getIcon ();
If (currenti! = NULL ){
Imageoffset = currenti. geticonwidth ()
+ Math. Max (0, geticontextgap ()-1 );
}
G. fillrect (imageoffset, 0, D. Width-1-imageoffset,
D. Height );
If (hasfocus ){
G. setcolor (uimanager
. Getcolor ("tree. selectionbordercolor "));
G. drawrect (imageoffset, 0, D. Width-1-imageoffset,
D. Height-1 );
}
}
}
Super. Paint (g );
}
Public dimension getpreferredsize (){
Dimension retdimension = super. getpreferredsize ();
If (retdimension! = NULL ){
Retdimension = new dimension (retdimension. Width + 3,
Retdimension. Height );
}
Return retdimension;
}
Public void setselected (Boolean isselected ){
This. isselected = isselected;
}
Public void setfocus (Boolean hasfocus ){
This. hasfocus = hasfocus;
}
}
}