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 / February 2004

Tip: Looking for answers? Try searching our database.

Custom ICON for certain nodes in JTree

Thread view: 
Michael - 09 Feb 2004 04:15 GMT
Hi,

I would like to set a custom icon next to a special node (for example,
a node that shows an exclaimation mark to show the status of this
node) in my JTree.

How can I go about doing that?
An example would be greatly appreciated as I am no GUI expert here :).
Chris Williams - 09 Feb 2004 19:52 GMT
1. override the DefaultTreeNode, say it is call CellRender, to make it
aware of what type of node it is (iconed or not and which icon),
something to the effect of isOnline or isOffline will work.

2. override the cellrenderer (extend DefaultTreeCellRenderer) and  
implement the following
public Component getTreeCellRendererComponent(JTree tree, Object
value,
                                                 boolean selected,
boolean expanded,
                                                 boolean leaf, int
row, boolean hasFocus) {}

    in there do something to the effect of the following

    if (((CellRender)value).isOnline)
        setIcon(<your icon>

that should do it.

> Hi,
>
[quoted text clipped - 6 lines]
> An example would be greatly appreciated as I am no GUI expert here
> :).
Jim Sculley - 11 Feb 2004 22:57 GMT
> 1. override the DefaultTreeNode, say it is call CellRender, to make it
> aware of what type of node it is (iconed or not and which icon),
> something to the effect of isOnline or isOffline will work.

Often it is better to make your own TreeModel implementation so that you
can avoid all the TreeNode nonsense.

> 2. override the cellrenderer (extend DefaultTreeCellRenderer) and  
> implement the following
[quoted text clipped - 10 lines]
>
> that should do it.

Generally, you first want to call the superclass implementation which
sets up a number of things such as highlighting, focus painting, etc.

super.getTreeCellRendererComponent(tree,value,sel,exp,leaf,row,hasFocus);
if (value instanceof SomeClassOfMine) {
    setIcon(someIcon);
}

return this;

Jim S.

Signature

Remove my extraneous mandibular appendages to reply via email.



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.