I recently read the source code of "Enterprise express mail". The friends list of this project is implemented using jtree.
After understanding its writing method, I want to familiarize myself with the use of the jtree control. Therefore, I wrote a small exercise to show that the username and image path in the database are in the jtree, and users can be added and displayed in the jtree.
Jtree contains the treecellrenderer interface, which can be implemented using classes. You can set the nodes in the tree in the corresponding method of this interface. For example, in this method, I set the node size and determine the node, and then display the node in a certain style.
Then, you can write a class that inherits to the jtree and set the root node defaultmutabletreenode root and defatretreemodel treemodel for it. Set its setcellrenderer to the class that inherits the treecellrenderer just written. Then you can add a node and use the root. Add (New defaultmutabletreenode (**) method and root. Remove () method to remove the node. It is worth noting that setmodel must be followed by setcellrenderer, and jpanel should be set to gridlayout () and then add jtree.
Database Connection method:
Package COM. LZW. dao; <br/> Import Java. SQL. *; <br/> Import COM. LZW. user. user1; <br/> Import Java. util. arraylist; <br/> Import Java. util. logging. level; <br/> Import Java. util. logging. logger; <br/>/** <br/> * @ author administrator <br/> */<br/> public class Dao {</P> <p> Public static connection getconnection () <br/>{< br/> connection conn = NULL; <br/> try <br/>{< br/> string driver = "com. microsoft. JDBC. sqlserver. sqlserverdriver "; <br/> string url =" JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = test "; <br/> string username =" sa "; <br/> string Pwd = "sa"; <br/> class. forname (driver); <br/> conn = drivermanager. getconnection (URL, username, PWD); <br/>}< br/> catch (exception ex) <br/>{< br/> system. out. println (ex. getmessage (); <br/>}< br/> return conn; <br/>}< br/> Public static void add User (user1 user) <br/>{< br/> string SQL = "insert into user1 values (?,?) "; <Br/> preparedstatement PSTM = NULL; <br/> connection conn = getconnection (); <br/> try <br/>{< br/> PSTM = Conn. preparestatement (SQL); <br/> PSTM. setstring (1, user. getUserName (); <br/> PSTM. setstring (2, user. getimageurl (); <br/> p0000.exe cute (); <br/>}< br/> catch (exception ex) <br/>{< br/> ex. printstacktrace (); <br/>}< br/> finally <br/>{< br/> try {<br/> PSTM. close (); <br/>} catch (sqlexcep Tion ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> try {<br/> Conn. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> Public static void deluser (user1 user) <br/> {<br/> string SQL = "delete user1 where username =? "; <Br/> preparedstatement PSTM = NULL; <br/> connection conn = getconnection (); <br/> try <br/>{< br/> PSTM = Conn. preparestatement (SQL); <br/> PSTM. setstring (1, user. getUserName (); <br/> p0000.exe cuteupdate (); <br/>}< br/> catch (exception ex) <br/>{< br/> ex. printstacktrace (); <br/>}< br/> finally <br/>{< br/> try {<br/> PSTM. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> try {<br/> Conn. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> Public static arraylist <user1> getalluser () <br/>{< br/> string SQL = "select * From user1"; <br/> arraylist <user1> List = new arraylist <user1> (); <br/> connection conn = getconnection (); <br/> statement STM = NULL; <br/> resultset rs = NULL; <br/> try <br/> {<br/> STM = Conn. createstatement (); <br/> rs1_0000.exe cutequery (SQL); <br/> while (RS. next () <br/>{< br/> user1 user = new user1 (); <br/> User. setusername (RS. getstring (2); <br/> User. setimageurl (RS. getstring (3); <br/> list. add (User); <br/>}< br/> catch (sqlexception ex) <br/>{< br/> ex. printstacktrace (); <br/>}< br/> finally <br/>{< br/> try {<br/> Rs. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> try {<br/> STM. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> try {<br/> Conn. close (); <br/>}catch (sqlexception ex) {<br/> logger. getlogger (Dao. class. getname ()). log (level. severe, null, ex); <br/>}< br/> return list; <br/>}< br/>
Classes corresponding to the user1 table:
Package COM. LZW. user; </P> <p>/** <br/> * @ author administrator <br/> */<br/> public class user1 {</P> <p> string username; <br/> string imageurl; <br/> Public void setusername (string name) <br/>{< br/> This. username = Name; <br/>}< br/> Public void setimageurl (string URL) <br/>{< br/> This. imageurl = URL; <br/>}< br/> Public String GetUserName () <br/>{< br/> return this. username; <br/>}< br/> Public String getimageurl () <br/>{< br/> return this. imageurl; <br/>}< br/>
Userrenderer class:
Package COM. LZW. user; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import javax. swing. *; <br/> Import javax. swing. tree. defaultmutabletreenode; <br/> Import javax. swing. tree. treecellrenderer; <br/>/** <br/> * @ author administrator <br/> */<br/> public class userrenderer extends jpanel implements treecellrenderer {<br /> private user1 user = NULL; <br/> private jlabel jb_username = ne W jlabel (); <br/> private jlabel jb_imageurl = new jlabel (); </P> <p> Public userrenderer () <br/>{< br/> super (); <br/> User = NULL; <br/>}< br/> Public userrenderer (icon open, icon close, icon leaf) <br/>{< br/> super (); <br/> This. setlayout (New borderlayout (); <br/>}< br/> Public component gettreecellrenderercomponent (jtree tree, object value, Boolean selected, Boolean expanded, Boolean leaf, int R Ow, Boolean hasfocus) {<br/> If (value instanceof defaultmutabletreenode) <br/>{< br/> defaultmutabletreenode dt = (defaultmutabletreenode) value; <br/> Object oo = DT. getuserobject (); <br/> If (OO instanceof user1) <br/>{< br/> User = (user1) Oo; <br/>}< br/> else if (value instanceof user1) <br/>{< br/> User = (user1) value; <br/>}< br/> If (user! = NULL) <br/>{< br/> // system. out. println (user. getUserName (); <br/> jb_username.settext (user. getUserName (); <br/> jb_imageurl.settext (user. getimageurl (); <br/> Add (jb_username, borderlayout. west); <br/> Add (jb_imageurl, borderlayout. center); <br/> This. setpreferredsize (new dimension (dimensions, 20); <br/>}< br/> return this; <br/>}</P> <p >}< br/>
Usertree class:
Package COM. LZW. user; <br/> Import COM. LZW. dao. dao; <br/> Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> Import Java. util. arraylist; <br/> Import Java. util. logging. level; <br/> Import Java. util. logging. logger; <br/> Import javax. swing. *; <br/> Import javax. swing. tree. defaultmutabletreenode; <br/> Import javax. swing. tree. defaulttreecellrenderer; <br/> Import javax. swing. tree. defaulttreemodel; <br/> Import javax. swing. tree. mutabletreenode; <br/> Import javax. swing. tree. treepath; <br/>/** <br/> * @ author administrator <br/> */<br/> public class usertree extends jtree {</P> <p> defaultmutabletreenode root; <br/> defaulttreemodel treemodel; <br/> Public usertree () <br/> {<br/> root = new defaultmutabletreenode ("root "); <br/> treemodel = new defaulttreemodel (Root); <br/> This. setrowheight (20); <br/> // This. setmodel (treemodel); <br/> This. settoggleclickcount (2); <br/> This. setrootvisible (false); <br/> defaulttreecellrenderer my = new defatretreecellrenderer (); <br/> userrenderer userrender = new userrenderer (my. getopenicon (), my. getclosedicon (), my. getleaficon (); <br/> This. setcellrenderer (userrender); <br/> This. setmodel (treemodel); <br/> sortusers (); <br/>}< br/> Public synchronized void sortusers () <br/>{< br/> New thread (New runnable () {<br/> Public void run () {<br/> try {<br/> thread. sleep (100); <br/> arraylist <user1> List = Dao. getalluser (); <br/> root. removeallchildren (); <br/> for (INT I = 0; I <list. size (); I ++) {<br/> user1 user = List. get (I); <br/> // system. out. println (user. getUserName (); <br/> root. add (New defaultmutabletreenode (User); <br/>}< br/> treemodel. reload (); <br/> usertree. this. setselectionrow (0); <br/>} catch (interruptedexception ex) {<br/> logger. getlogger (usertree. class. getname ()). log (level. severe, null, ex); <br/>}< br/> }). start (); <br/>}< br/> Public void adduser (user1 user) <br/>{< br/> Dao. adduser (User); </P> <p> root. add (New defaultmutabletreenode (User); <br/> treemodel. reload (); <br/>}< br/> Public void deluser () <br/>{< br/> treepath Path = getselectionpath (); <br/> user1 user = (user1) (defaultmutabletreenode) path. getlastpathcomponent ()). getuserobject (); <br/> root. remove (New defaultmutabletreenode (User); <br/> treemodel. reload (); <br/> Dao. deluser (User); <br/>}< br/>
Interface:
Package COM. LZW. frame; </P> <p> Import COM. LZW. user. user1; <br/> Import COM. LZW. user. usertree; <br/> Import Java. AWT. gridlayout; </P> <p>/** <br/> * @ author administrator <br/> */<br/> public class mainframe extends javax. swing. jframe {</P> <p> private usertree mytree; <br/>/** creates new form mainframe */<br/> Public mainframe () {<br/> initcomponents (); <br/> mytree = new usertree (); <br/> This. jpanel1.setlayout (New gridlayout (1, 1); <br/> This. jpanel1.add (mytree); <br/>}</P> <p>/** this method is called from within the constructor to <br/> * initialize the form. <br/> * warning: do not modify this code. the content of this method is <br/> * always regenerated by the form editor. <br/> */<br/> @ suppresswarnings ("unchecked ") <br/> // <Editor-fold defaultstate = "Collapsed" DESC = "generated code"> <br/> private void initcomponents () {</P> <p> jlabel1 = new javax. swing. jlabel (); <br/> jtextfield1 = new javax. swing. jtextfield (); <br/> jlabel2 = new javax. swing. jlabel (); <br/> jtextfield2 = new javax. swing. jtextfield (); <br/> jpanel1 = new javax. swing. jpanel (); <br/> jbutton1 = new javax. swing. jbutton (); </P> <p> setdefaclocloseoperation (javax. swing. windowconstants. exit_on_close); </P> <p> jlabel1.settext ("username:"); <br/> jlabel1.setname ("jlabel1 "); // noi18n </P> <p> jtextfield1.setname ("jtextfield1"); // noi18n </P> <p> jlabel2.settext ("image path :"); <br/> jlabel2.setname ("jlabel2"); // noi18n </P> <p> jtextfield2.setname ("jtextfield2 "); // noi18n </P> <p> jpanel1.setname ("jpanel1"); // noi18n </P> <p> javax. swing. grouplayout jpanel1layout = new javax. swing. grouplayout (jpanel1); <br/> jpanel1.setlayout (jpanel1layout); <br/> jpanel1layout. sethorizontalgroup (<br/> jpanel1layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgap (0,380, short. max_value) <br/>); <br/> jpanel1layout. setverticalgroup (<br/> jpanel1layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgap (0,316, short. max_value) <br/>); </P> <p> jbutton1.settext ("add"); <br/> jbutton1.setname ("jbutton1 "); // noi18n <br/> jbutton1.addactionlistener (New Java. AWT. event. actionlistener () {<br/> Public void actionreceivmed (Java. AWT. event. actionevent EVT) {<br/> jbutton1actionreceivmed (EVT); <br/>}< br/>}); </P> <p> javax. swing. grouplayout layout = new javax. swing. grouplayout (getcontentpane (); <br/> getcontentpane (). setlayout (layout); <br/> layout. sethorizontalgroup (<br/> layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgroup (layout. createsequentialgroup () <br/>. addgap (50, 50, 50) <br/>. addgroup (layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgroup (layout. createsequentialgroup () <br/>. addcomponent (jpanel1, javax. swing. grouplayout. preferred_size, javax. swing. grouplayout. default_size, javax. swing. grouplayout. preferred_size) <br/>. addcontainergap () <br/>. addgroup (layout. createsequentialgroup () <br/>. addgroup (layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgroup (layout. createsequentialgroup () <br/>. addcomponent (jlabel2) <br/>. addgap (18, 18, 18) <br/>. addcomponent (jtextfield2, javax. swing. grouplayout. preferred_size, 0, short. max_value) <br/>. addgroup (layout. createsequentialgroup () <br/>. addcomponent (jlabel1) <br/>. addgap (29, 29, 29) <br/>. addcomponent (jtextfield1, javax. swing. grouplayout. default_size, 175, short. max_value) <br/>. addgap (31, 31, 31) <br/>. addcomponent (jbutton1) <br/>. addgap (52, 52, 52) <br/>); <br/> layout. setverticalgroup (<br/> layout. createparallelgroup (javax. swing. grouplayout. alignment. leading) <br/>. addgroup (layout. createsequentialgroup () <br/>. addcontainergap () <br/>. addgroup (layout. createparallelgroup (javax. swing. grouplayout. alignment. baseline) <br/>. addcomponent (jlabel1) <br/>. addcomponent (jtextfield1, javax. swing. grouplayout. preferred_size, javax. swing. grouplayout. default_size, javax. swing. grouplayout. preferred_size) <br/>. addgap (33, 33, 33) <br/>. addgroup (layout. createparallelgroup (javax. swing. grouplayout. alignment. baseline) <br/>. addcomponent (jlabel2) <br/>. addcomponent (jtextfield2, javax. swing. grouplayout. preferred_size, javax. swing. grouplayout. default_size, javax. swing. grouplayout. preferred_size) <br/>. addcomponent (jbutton1) <br/>. addgap (32, 32, 32) <br/>. addcomponent (jpanel1, javax. swing. grouplayout. preferred_size, javax. swing. grouplayout. default_size, javax. swing. grouplayout. preferred_size) <br/>. addcontainergap (31, short. max_value) <br/>); </P> <p> pack (); <br/>}// </Editor-fold> </P> <p> private void jbutton1actionreceivmed (Java. AWT. event. actionevent EVT) {<br/> // todo add your handling code here: <br/> user1 user = new user1 (); <br/> User. setusername (jtextfield1.gettext (). trim (); <br/> User. setimageurl (jtextfield2.gettext (). trim (); <br/> mytree. adduser (User ); <br/>}</P> <p>/** <br/> * @ Param ARGs the command line arguments <br/> */<br/> Public static void main (string ARGs []) {<br/> JAVA. AWT. eventqueue. invokelater (New runnable () {<br/> Public void run () {<br/> New mainframe (). setvisible (true); <br/>}< br/> }); <br/>}</P> <p> // variables Declaration-do not modify <br/> private javax. swing. jbutton jbutton1; <br/> private javax. swing. jlabel jlabel1; <br/> private javax. swing. jlabel jlabel2; <br/> private javax. swing. jpanel jpanel1; <br/> private javax. swing. jtextfield jtextfield1; <br/> private javax. swing. jtextfield jtextfield2; <br/> // end of variables declaration </P> <p >}< br/>