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 / General / January 2006

Tip: Looking for answers? Try searching our database.

custom JTree icons

Thread view: 
Brandon McCombs - 13 Jan 2006 03:30 GMT
I have figured out how to set my own custom icons for my JTree entries
however I want to have a certain icon based on what type of entry it is,
not where it is. Is this possible? I want one icon for the root, another
icon for internal nodes (and I can have different types of internal
nodes that are application specific so I want different icons), and
another type for leaf nodes.

thanks
Thomas Hawtin - 13 Jan 2006 08:30 GMT
> I have figured out how to set my own custom icons for my JTree entries
> however I want to have a certain icon based on what type of entry it is,
> not where it is. Is this possible? I want one icon for the root, another
> icon for internal nodes (and I can have different types of internal
> nodes that are application specific so I want different icons), and
> another type for leaf nodes.

It's in the tutorial.

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Brandon McCombs - 13 Jan 2006 15:19 GMT
>> I have figured out how to set my own custom icons for my JTree entries
>> however I want to have a certain icon based on what type of entry it
[quoted text clipped - 8 lines]
>
> Tom Hawtin

I found something similar to that later on after I posted my message but
it doesn't work for me. I'll show you where my problem is. Here is the
tutorial's code for the cell renderer for reference:

private class MyRenderer extends DefaultTreeCellRenderer {
        Icon tutorialIcon;

        public MyRenderer(Icon icon) {
            tutorialIcon = icon;
        }

        public Component getTreeCellRendererComponent(
                            JTree tree,
                            Object value,
                            boolean sel,
                            boolean expanded,
                            boolean leaf,
                            int row,
                            boolean hasFocus) {

            super.getTreeCellRendererComponent(
                            tree, value, sel,
                            expanded, leaf, row,
                            hasFocus);
            if (leaf && isTutorialBook(value)) {
                setIcon(tutorialIcon);
                setToolTipText("This book is in the Tutorial series.");
            } else {
                setToolTipText(null); //no tool tip
            }

            return this;
        }

        protected boolean isTutorialBook(Object value) {
            DefaultMutableTreeNode node =
                    (DefaultMutableTreeNode)value;
            BookInfo nodeInfo =
                    (BookInfo)(node.getUserObject());
            String title = nodeInfo.bookName;
            if (title.indexOf("Tutorial") >= 0) {
                return true;
            }

            return false;
        }
    }

My code compiles but during runtime I get a class cast exception here:
"BookInfo nodeInfo = (BookInfo)(node.getUserObject());"
The error says I can't convert the String returned from getUserObject()
to, in my case, a Node object so how can this code even work?  I looked
at their BookInfo class and they don't override the setUserObject()
method to make it store anything other than a String so how can they get
the string to cast to a BookInfo instance and I can't get my string to
cast to a Node instance? In fact their BookInfo class doesn't even
extend the DefaultMutableTreeNode class so how can they even make the
"value" argument cast to that? It doesn't make sense to me.

Here is my Node class:
public class Node extends DefaultMutableTreeNode {
        private static final long serialVersionUID = 0L;
        //local DN is needed to more easily keep track of DN paths
        private String DN = null;
        public Node(NameClassPair entryName, String newDN) {
                    super(entryName.getName());
                    DN = newDN;
        }
       
        public Node(NameClassPair entryName) {
            super(entryName.getName());
            DN = entryName.getName();
        }
       
        public Node(String entryName) {
            super(entryName);
        }
       
        public String getDN() {
            return DN;
        }
       
        public void setDN(String newDN) {
            DN = newDN;
        }
}

thanks for your help
Thomas Hawtin - 14 Jan 2006 18:37 GMT
>         protected boolean isTutorialBook(Object value) {
>             DefaultMutableTreeNode node =
>                     (DefaultMutableTreeNode)value;
>             BookInfo nodeInfo =
>                     (BookInfo)(node.getUserObject());

> My code compiles but during runtime I get a class cast exception here:
> "BookInfo nodeInfo = (BookInfo)(node.getUserObject());"
[quoted text clipped - 9 lines]
> Here is my Node class:
> public class Node extends DefaultMutableTreeNode {

The Swing Tutorial code is using the user object (set through the
constructor) to hold the data. No need to extend, let alone override,
setUserObject. You are extending the TreeNode to store additional data.
So switch to either using the user object property throughout, or to
extending TreeNode and ignoring user object.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Thomas Weidenfeller - 13 Jan 2006 13:22 GMT
> I want one icon for the root, another
> icon for internal nodes (and I can have different types of internal
> nodes that are application specific so I want different icons), and
> another type for leaf nodes.

See the comp.lang.java.gui FAQ.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/



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



©2009 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.