Hi Josip,

You can do it with a renderer but you need to discriminate the node by its
value.

See the snippet below.

Thanks and regards,

Janak

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] Behalf Of pr0v4
Sent: Wednesday, August 22, 2007 10:54 AM
To: [email protected]
Subject: [ULC-developer] Fwd: ULCTree




---------- Forwarded message ----------
From: pr0v4 <[EMAIL PROTECTED]>
Date: 22-Aug-2007 10:42
Subject: ULCTree
To: [email protected]

I'm trying to use ULCTree component. What I need is dynamically change of
ULCTree node Icons, if someone can give me any kind of example wich will
give me solution I will be thankful. I know that ULCTree can dynamically
change node Icons by selection, request focus .... in short by it's own
events. But still he changes all node Icons like OpenIcon, ClosedIcon,
LeafIcon. What I need is to change single node Icon from applicaton event,
not from the Renderer component.... So please if somone have Idea....
Josip,
Thx in advance!!

---------------------------------


import com.ulcjava.base.application.AbstractApplication;
import com.ulcjava.base.application.IRendererComponent;
import com.ulcjava.base.application.ULCBoxPane;
import com.ulcjava.base.application.ULCButton;
import com.ulcjava.base.application.ULCFrame;
import com.ulcjava.base.application.ULCLabel;
import com.ulcjava.base.application.ULCTree;
import com.ulcjava.base.application.event.ActionEvent;
import com.ulcjava.base.application.event.IActionListener;
import com.ulcjava.base.application.tree.DefaultMutableTreeNode;
import com.ulcjava.base.application.tree.DefaultTreeCellRenderer;
import com.ulcjava.base.application.tree.DefaultTreeModel;
import com.ulcjava.base.application.util.Color;
import com.ulcjava.base.application.util.ULCIcon;
import com.ulcjava.base.development.DevelopmentRunner;

public class DynamicTreeCellRenderSnippet extends AbstractApplication {
    final private ULCIcon[] fIconArray = new ULCIcon[3];
    private int fCounter;
    private DefaultMutableTreeNode fChild1Node;

    public void start() {
        fIconArray[0] = new
ULCIcon(DynamicTreeCellRenderSnippet.class.getResource("/ascending.gif"));
        fIconArray[1] = new
ULCIcon(DynamicTreeCellRenderSnippet.class.getResource("/descending.gif"));
        final DefaultMutableTreeNode root = new DefaultMutableTreeNode(new
MyUserObject("AAAAA", false));
        fChild1Node = new DefaultMutableTreeNode(new MyUserObject("BBBBB",
true), true);
        root.add(fChild1Node);
        root.add(new DefaultMutableTreeNode(new MyUserObject("CCCCC",
false), true));

        ULCBoxPane contentPane = new ULCBoxPane(true);
        final ULCTree tree = new ULCTree();
        final DefaultTreeModel treeModel = new DefaultTreeModel(root);
        tree.setModel(treeModel);

        tree.setCellRenderer(new MyTreeCellRenderer());
        tree.setEditable(true);

        contentPane.add(ULCBoxPane.BOX_EXPAND_EXPAND, tree);

        ULCButton button = new ULCButton("Set Dynamic Icon");
        button.addActionListener(new IActionListener() {
            public void actionPerformed(ActionEvent event) {
                tree.setCellRenderer(new DynamicTreeCellRenderer(fCounter %
2));
                treeModel.nodeChanged(fChild1Node);
                fCounter++;
            }
        });

        contentPane.add(ULCBoxPane.BOX_EXPAND_CENTER, button);

        ULCFrame frame = new ULCFrame("DynamicTreeCellRenderSnippet");
        frame.setDefaultCloseOperation(ULCFrame.TERMINATE_ON_CLOSE);
        frame.setSize(200, 200);
        frame.add(contentPane);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        DevelopmentRunner.setApplicationClass(DynamicTreeCellRenderSnippet.c
lass);
        DevelopmentRunner.main(args);
    }

    public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
        public MyTreeCellRenderer() {
        }

        public IRendererComponent getTreeCellRendererComponent(ULCTree tree,
Object value, boolean selected,
                boolean expanded, boolean leaf, boolean hasFocus) {

            ULCLabel label =
(ULCLabel)super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf,
                    hasFocus);
            DefaultMutableTreeNode node = ((DefaultMutableTreeNode)value);
            MyUserObject element = (MyUserObject)node.getUserObject();
            label.setText(element.toString());
            label.setBackground(Color.yellow);
            return this;
        }
    }

    public class DynamicTreeCellRenderer extends DefaultTreeCellRenderer {
        private int fIconIndex;

        public DynamicTreeCellRenderer(int icon) {
            fIconIndex = icon;
        }

        public IRendererComponent getTreeCellRendererComponent(ULCTree tree,
Object value, boolean selected,
                boolean expanded, boolean leaf, boolean hasFocus) {

            ULCLabel label =
(ULCLabel)super.getTreeCellRendererComponent(tree, value, selected,
expanded, leaf,
                    hasFocus);
            DefaultMutableTreeNode node = ((DefaultMutableTreeNode)value);
            MyUserObject element = (MyUserObject)node.getUserObject();
            if (element.toString().equals("BBBBB")) {
                label.setIcon(fIconArray[fIconIndex]);
                label.setText(element.toString());
                label.setBackground(Color.yellow);
            }
            return label;
        }
    }

    class MyUserObject {
        String text;
        boolean selected;

        public MyUserObject(String text, boolean selected) {
            this.text = text;
            this.selected = selected;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setSelected(boolean newValue) {
            selected = newValue;
        }

        public String getText() {
            return text;
        }

        public void setText(String newValue) {
            text = newValue;
        }

        public String toString() {
            return text;
        }
    }
}

_______________________________________________
ULC-developer mailing list
[email protected]
http://lists.canoo.com/mailman/listinfo/ulc-developer

Reply via email to