GUI Tree components, tables

Source: Internet
Author: User

The tree component first needs a new jtree, and then adds nodes to the JScrollPane.

JTree tree1=New  jtree (); // ....... Add a node Add (new ScrollPane (TREE1)

Adding nodes

Defaultmutabletreenode root=New defaultmutabletreenode ("Dongxi")// followed by notes Defaultmutabletreenode root=New defaultmutabletreenode (new Good ("AA", 11)); // It can also be used as a leaf new object

Add a monitor (added by the tree)

Addtreeselectionlistener (Treeselectionlistener Listener)

Method of Treeselectionlistener interface

 Public void valuechanged (treeselectionevent e)

In the above interface method, returns the node of the tree and returns the object in the node.

Defaultmutabletreenode node1= (Defaultmutabletreenode) tree.getlastselectedpathcomponent (); // only tree trees can use it. if (Node.isleaf ()) // determine if the leaf node Obeject a= (Obeject) node.getuserobject ()// return the object inside the leaf

Complete test Code

 Public classtest{ Public Static voidMain (String args[]) {Mywin Window1=NewMywin (); Window1.setbounds (12,12,400,300); }}classMywinextendsJFrameImplementstreeselectionlistener{JTextField Text1;    JButton Button1,button2;    JTextArea textarea1,textarea2;    JLabel Label1;    JTree tree1;        Mywin () {init (); SetVisible (true);    Setdefaultcloseoperation (Jframe.exit_on_close); }    voidinit () {Defaultmutabletreenode root=NewDefaultmutabletreenode ("Goods"); Defaultmutabletreenode Nodetv=NewDefaultmutabletreenode ("TV"); Defaultmutabletreenode Nodephone=NewDefaultmutabletreenode ("Phone"); Defaultmutabletreenode TV1=NewDefaultmutabletreenode (NewGoods ("Huangtv", 11)); Defaultmutabletreenode TV2=NewDefaultmutabletreenode (NewGoods ("Yutv", 21)); Defaultmutabletreenode Phone1=NewDefaultmutabletreenode (NewGoods ("Ga", 31)); Defaultmutabletreenode Phone2=NewDefaultmutabletreenode (NewGoods ("Bing", 11));        Root.add (NODETV);        Root.add (Nodephone);        Nodetv.add (TV1);        Nodetv.add (TV2);        Nodephone.add (PHONE1);        Nodephone.add (Phone2); Tree1=NewJTree (root); Tree1.addtreeselectionlistener ( This); SetLayout (NewGridLayout ()); Add (NewJScrollPane (tree1)); TEXTAREA2=NewJTextArea (15,15); Add (NewJScrollPane (TEXTAREA2)); }     Public voidvaluechanged (treeselectionevent e) {Defaultmutabletreenode a=(Defaultmutabletreenode) tree1.getlastselectedpathcomponent (); if(A.isleaf ()) {//is it a leaf node? Goods b=(Goods) a.getuserobject (); Textarea2.append (B.name+ ":" +b.price+ "\ n"); }        ElseTextarea2.settext (NULL); }}classgoods{String name; DoublePrice ; Goods (String name,DoublePrice ) {         This. name=name;  This. price=Price ; }}

GUI Tree components, tables

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.