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 2003

Tip: Looking for answers? Try searching our database.

Multiple Icons on the same JTree Node

Thread view: 
Dan Reese - 29 Dec 2003 18:43 GMT
Hello,

I have a requirement to display multiple icons on a single JTree node.
Does anyone know if this capability is supported in the Swing JTree,
if it can be easily added to the Swing JTree or if there is a
commercial tree that supports this behavior?

Thanks.

Dan
Karsten Lentzsch - 29 Dec 2003 19:18 GMT
> I have a requirement to display multiple icons on a single JTree node.
>  Does anyone know if this capability is supported in the Swing JTree,
> if it can be easily added to the Swing JTree or if there is a
> commercial tree that supports this behavior?

One can do this quite easily with a JTree.
You can set a custom tree cell renderer that
paints the tree cell - and you can do almost
everything in that renderer.

In simple cases, the default tree cell renderer
will do the job too. For example, if you want to
overlap a main icon with annotation icons, you can
just implement a custom Icon class that renders
the main icon plus overlaps and put this into
the default tree cell renderer.

Hope this helps,
Karsten
Steve W. Jackson - 29 Dec 2003 20:35 GMT
>:Dan Reese wrote:
>:
[quoted text clipped - 17 lines]
>:Hope this helps,
>:Karsten

We've got our own subclass of JTree which uses a private subclass of
DefaultTreeCellRenderer (as in, it's a private inner class in our
subclass of JTree).  And the renderer class already contains a setIcon()
method, so we're merely constructing an icon that may be a combination
of several items, then passing it to that method.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Dan Reese - 30 Dec 2003 17:40 GMT
> >:Dan Reese wrote:
> >:
[quoted text clipped - 25 lines]
>
> = Steve =

Thanks for the info. A question for Steve, when you say you are
constructing an icon, how are you doing that?  I haven't done any GUI
Java, only server side so I'm a little fuzzy on some of this.

What I want is really pretty simple.  Most of the nodes on the tree
will have a single icon, albeit there are 4 or 5 different icons that
can be used.  When a user checks out one of the nodes, I just need to
stick a key icon beside the original icon.

Thanks again.

Dan
Steve W. Jackson - 30 Dec 2003 18:50 GMT
>:> In article <bspum7$7q5$03$1@news.t-online.com>,
>:>  Karsten Lentzsch <Karsten@JGoodies.com> wrote:
[quoted text clipped - 41 lines]
>:
>:Dan

I don't do any server-side, so I can't say what would translate well and
what wouldn't.  But here's basically what we do.

We've got a class that extends JTree.  It contains two inner classes.

The first of these is used to carry information about each node on the
tree, so that it can be a fairly complex structure with references back
to the XML document that represents our data model.  One of the pieces
of data it carries is its icon (and ours don't change once the nodes are
initially created).

The second inner class extends DefaultTreeCellRenderer.  It really
doesn't do much except provide a method for getting access to the
underlying info structure (the instance of the other inner class) for
each node, and it has some logic to set the tooltip and set the icon
specified in that info structure.

It's important to understand that setting the icon for each node, as we
do it, uses the existing setIcon() method in DefaultTreeCellRenderer,
which takes a parameter of type Icon (an interface).  Our info structure
carries an object of type ImageIcon.  So we construct that object when a
node's info structure is built by combining some pre-existing images
into a single one.  I didn't write this code, so I'm doing a quick
paraphrase here, but basically it reads some stored image files that are
included with our app (in this case, all are GIF), draws them to a
BufferedImage's Graphics2D, then creates a single ImageIcon object from
that combined image.

I'm not particularly graphically literate, so it's a good thing somebody
else on our team was when it came time to do some of the drawing stuff.  
But that's basically how it worked out in our case.

Hope this helps.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama



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.