Results 1 to 4 of 4
  1. #1
    Nidi is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default Adding Icons to JTree

    Hello...

    Can any one help me out to slove this problem...

    I am creating Jtree in a UI. I need to add same icons to one parent node and for different parent node, different icons. I used below code...

    DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();
    renderer3.setLeafIcon(customLeafIcon);
    tree3.setCellRenderer(renderer3);

    After using this same icon is displaying for all parent node...:confused:

    can any one tell me what is the mistake....

    Thanks in advance
    nidi
    :)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nidi View Post
    Hello...

    Can any one help me out to slove this problem...

    I am creating Jtree in a UI. I need to add same icons to one parent node and for different parent node, different icons. I used below code...

    DefaultTreeCellRenderer renderer3 = new DefaultTreeCellRenderer();
    renderer3.setLeafIcon(customLeafIcon);
    tree3.setCellRenderer(renderer3);

    After using this same icon is displaying for all parent node...:confused:

    can any one tell me what is the mistake....

    Thanks in advance
    nidi
    :)
    Extend the DefaultTreeCellRenderer class and override the following method:

    Java Code:
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
       ...
    }
    The second parameter is the current tree node object; according to its type you set a different icon using the setIcon( ... ) method (a renderer extends a JLabel so you can set their icon).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Nidi is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you Jos.... I will definitely try this method.... :)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nidi View Post
    Thank you Jos.... I will definitely try this method.... :)
    Good; a skeleton of the method you need to implement in your class looks like this:

    Java Code:
    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);
    
    	// setIcon( ... ) according the the type of value
    
    	return this;
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. New Posts Icons
    By desertlizard in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 05:00 PM
  2. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  3. [SOLVED] Adding JTree, JTable inside a JTabbedPane
    By javanewbie in forum AWT / Swing
    Replies: 6
    Last Post: 05-28-2009, 05:32 AM
  4. how to compare icons
    By panic in forum New To Java
    Replies: 9
    Last Post: 05-21-2009, 08:16 AM
  5. is there anyone how can help me with image icons please?
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •