CheckBoxTree with Icons and lazy loading

This is the forum for JIDE Common Layer which is open sourced at https://github.com/jidesoft/jide-oss. Please note, JIDE technical support doesn't monitor this forum as often as other forums. Please consider subscribe for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

Moderator: JIDE Support

Forum rules
Community driven forum for open source JIDE Common Layer. JIDE technical support doesn't monitor this forum as often as other forums. If you only use JIDE Common Layer, please consider subscribing for technical support for JIDE Common Layer so that you can use customer only forum to get a timely response.

CheckBoxTree with Icons and lazy loading

Postby nick.denton » Thu Nov 17, 2011 8:13 am

Hi there,

I am trying to create a checkboxtree with different icons for some nodes regarding their value, but it seems the icons dont show up. I really don't know what I am doing wrong. I have a class MyTreeCellRenderer which extends the DefaultTreeCellRenderer. The class looks like that:

Code: Select all
public class MyTreeCellRenderer implements TreeCellRenderer{

    private JLabel label;
   
    public MyTreeCellRenderer(){
        label = new JLabel();
    }
   
   
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
       
        Object o = ((DefaultMutableTreeNode) value).getUserObject();
            if (o instanceof MyNode) {
               
                MyNode myNode = (MyNode) o;
             
                label.setText(myNode.getName());
                  label.setIcon(myNode.geImage());
             
               
            } else {
               
               label.setText("" + value);
            }
            return label;
       
    }
   
}


Is something wrong??
nick.denton
 
Posts: 7
Joined: Wed Nov 16, 2011 6:02 pm

Re: CheckBoxTree with Icons and lazy loading

Postby JIDE Support » Thu Nov 17, 2011 9:45 am

It does work for me in 3.2.6. The test case I used is attached FYI.

Thanks,
Attachments
CheckBoxTreeDemo.java
(12.16 KiB) Downloaded 1405 times
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am

Re: CheckBoxTree with Icons and lazy loading

Postby nick.denton » Thu Nov 17, 2011 11:09 am

Hi and thanks a lot for the quick answer.

I managed to show the Icon and the text but I have another issue now. It seems that it puts the icon to all the nodes plus leafs. Even if I put many if statements at the end the icon is everywhere.
example:
Code: Select all

public class MyTreeCellRenderer implements TreeCellRenderer{
    private JLabel label;
    public MyTreeCellRenderer(){
        label = new JLabel();
    }
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        String o = (String) ((DefaultMutableTreeNode) value).getUserObject();
        if(o.matches("Something")){
                     label.setText(o);
                    label.setIcon(new ImageIcon(myIcon)));
                    return label;
         }
         else {   
               label.setText(o);
                return label;
            }   
    }   
}


Even if the node value its not equal with "Something" at the end it will have the icon. Any idea?

Thanks,

nick
nick.denton
 
Posts: 7
Joined: Wed Nov 16, 2011 6:02 pm

Re: CheckBoxTree with Icons and lazy loading

Postby JIDE Support » Thu Nov 17, 2011 11:14 am

Based on Swing design, the renderer component is reused by all nodes on painting so please invoke setIcon(null) in the else branch.

Thanks,
JIDE Software Technical Support Team
JIDE Support
Site Admin
 
Posts: 37219
Joined: Sun Sep 14, 2003 10:49 am


Return to JIDE Common Layer Open Source Project Discussion (Community Driven)

Who is online

Users browsing this forum: No registered users and 16 guests

cron