Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2005

Tip: Looking for answers? Try searching our database.

JTree only display certain part of node

Thread view: 
norki - 21 Dec 2005 00:24 GMT
Hi,

Is there anyway to create a JTree with objects where only a certain
field from the object is displayed for the node.

I.e.

public someObject {
    public String displayThis;
    public int someKey;
}

I only want 'displayThis' field to appear in my GUI but still be able to
retrieve the Object from the node and get the 'someKey' value.

Any help appreciated,
norki
BartCr - 21 Dec 2005 06:13 GMT
Write your own TreeCellRenderer
(http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/tree/TreeCellRenderer.html)
and install it on your JTree (tree.setCellRenderer(myRenderer)).

Bart
mera - 21 Dec 2005 08:18 GMT
> Hi,
>
[quoted text clipped - 13 lines]
> Any help appreciated,
> norki

You can write your own DefaultMutableTreeNode:

public class MyDefaultMutableTreeNode extends DefaultMutableTreeNode {

    public String displayThis;
    public int someKey;

    //constructor
    public GuiConsoleNode () {
    }//end constructor

    public GuiConsoleNode (String displayThis, int someKey) {
        super(displayThis);
                               this.someKey = someKey;
    }//end constructor
mera - 21 Dec 2005 08:27 GMT
> > Hi,
> >
[quoted text clipped - 29 lines]
>                                 this.someKey = someKey;
>     }//end constructor

Made a mistake in the constructor name. This is the correct version:

public class MyDefaultMutableTreeNode extends DefaultMutableTreeNode {

     public String displayThis;
     public int someKey;

    //constructor
    public MyDefaultMutableTreeNode() {
    }//end constructor

    public MyDefaultMutableTreeNode(String displayThis, int someKey) {
        super(displayThis);
                                this.someKey = someKey;
    }//end constructor
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.