aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes how can I change this tree with different Icons for Different nodes at Run Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how can I change this tree with different Icons for Different nodes at Run Time" Watch "how can I change this tree with different Icons for Different nodes at Run Time" New topic
Author

how can I change this tree with different Icons for Different nodes at Run Time

Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Dear sir:
One more question again,
I use following code to set icon for the each node
here I assume customLeafIcon has Icon of "Bird.GIF"



but for the leaf nodes, I have different Icons for different components, For Ex. at RUN Time, If I drop a leaf Node with TextString called "cat" in this Tree, then I need only Cat Icon for this node dropped, same, If I drop a leaf Node with TextString called "tiger" in this Tree, then I need only tiger Icon for this node dropped, and so on, but above mentioned code will change all leaf nodes with SAME Icon of "Bird.GIF", how can I change this tree with different Icons for Different nodes at Run Time, not Design Time??

Thanks in Advance.
Michelle
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
In the section Customizing a Tree's Display there is an example named TreeIconDemo2 and short discussion about changing leaf icons according to the value of the leaf node at runtime.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Hello Sir:
Thanks for your advice, I follow your link and study the part,
it is very good, but I met a new problem.
I used the following code from this example:



I rewrite class MyRenderer and extend DefaultTreeCellRenderer,
at initial, it successes to put the Icon I need to the specific nodes/leaves; but when I drag n drop other nodes/leaves with different Icon, all nodes replaced with latest nodes'/leaves' Icon.

What is wrong here??
How to keep all previous nodes'/leaves' icons without updating until I require to do so??
Thanks so much.
Michelle
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Can Somebody throw some light on this issue?? I tried all day yesterady, but failed. Thanks a lot.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
when I drag n drop other nodes/leaves with different Icon, all nodes replaced with latest
nodes'/leaves' Icon

Does this mean that after any drag_n_drop other nodes have their icon changed to the icon of
the node that was just dropped? If so the example below might be helpful. If not, then I
don't understand your question.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Does this mean that after any drag_n_drop other nodes have their icon changed to the icon of the node that was just dropped?

Yes, this is what I saw.
thanks so much, it is really exciting. Top code!! can be used for many purpose.
[ May 11, 2007: Message edited by: girl sunny ]
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Dear Sir:
Good weekends, I found another example as follows:

here animalIcon is the Icon of coming node I drag and drop into this JTree,
leafIcon is a common leaf Icon for all leaf nodes;

I use following categories:

[1]. Human -
a. whiteman - whitemanIcon
b. blackman - blackmanIcon
c. redman - redmanIcon

[2]. Fish -
a. goldFish - goldFishIcon
b. swordFish - swordFishIcon
c. shark - sharkIcon
d. catFish - catFishIcon

[3]. Dog -
a. AsianDog - AsianDogIcon
b. USDog - USDogIcon
c. EuroDog - EuroDogIcon

I use following code to implement DnD nodes into JTree, so all existing nodes and their Icons will be kept unchanged after coming node DnD finishes;


I tried this code but found out that the icon of new coming node replaces all other icons of original nodes, I tried to use Craig's code to update code to meet this scenario, but not success.

When I tried this program:
[1]. I Drag and drop whiteman node into this JTree, then coming node and all leaves' icon become whitemanIcon;
[2]. then I Drag and drop swordFish node into this JTree, then coming node and all leaves' icon become swordFishIcon;
[3]. at last I Drag and drop EuroDog node into this JTree, then coming node and all leaves' icon become EuroDogIcon;

because I only drag and drop whiteman,swordFish, EuroDogIcon three nodes, I need these three whitemanIcon,swordFishIcon, EuroDogIcon to attach three diifferent nodes, not one for all, but they all gone, only remain last EuroDogIcon, what is wrong here??

Thanks a lot!!

Michelle
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Hi, Craig:
How to update your code meet my above-mentioned example or update my example to meet your excellent sample?? I tried this weekends, but still get all icons replaced by last DnD node's Icon.
Thanks
Michelle
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Some general comments.
The cell renderer for a tree is used to render every node in the tree. So it must have
access to any_icon/all_icons needed for the nodes in the tree. There are two ways you
could approach this. Keep a complete set of icons in the cell renderer and give it the
information it needs to be able to recognize which icon is to be set for each leaf node.
Calling super will take care of setting the default icons for the the tree so you won't
have to be concerned with the rest of the (non-leaf) nodes.
The other way you could do this is to create a UserObject that will include icons so the
cell renderer can get the icon that way, ie, directly from the user object. This would
enable you to drag new/unknown icons into the tree and have the renderer set them. You
would make a new user object class instance for each new/dropped node (which we do anyway)
and include the incoming icon in it.
Since you have a detailed list of possible icons and since the first option is much easier
to set up let's consider it.

With this idea the user object can be a simple string. And the drag_n_drop code can remain
the same.

If you still need a more complicated user object then we should consider its design and
use.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Thanks a lot, Craig, really good,
When I input the code, it prpmpts
"Syntax Error,parameterized Types Only available if source level is 5.0";

Map <String, ImageIcon> iconMap
;

My JDK is 6, can we have some way other than JDK5.0??
Thanks
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
some way other than JDK5.0
Here is the non–generic code to use.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Great, Thanks so much, success!!
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Another problem occurs, for any coming DnD CustomImageIcon, generally I use CustomImageIcon.getToolTipText() to get default/original name so it totally separate CustomImageIcon.getname() and customImageIcon.getToolTipText(), so when I change the name of coming DnD CustomImageIcon, I only change the value of CustomImageIcon's TextString, not CustomImageIcon.getToolTipText()'s value, but here If I change the values of each coming DnD's nodes' names, then their Icons all become default icons, because node.getUserObject().toString() change the value and did not contain the identical string needed to make decision.
Any idea to solve this issue??



For example, here:

when I change he JTree node which contains string "Fish" to "Myfavorite",
then Icon Fish is gone and replaced by deafult icon.

Actually, I use tree.setEditable(true) to change value of each node with success, but I hope to keep the icon unchanged, only change name. how to do it?
Thanks.
[ May 16, 2007: Message edited by: girl sunny ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Each time the tree or a leaf is rendered your renderer must be able to find out which icon
(or no icon) goes for each leaf node. Even if you drop in other strings that have no
corresponding icon. So you'll have to build a data structure that can remember the icons for
each leaf node. You can get and set the values (icons) for any given leaf node. You could
consider a Map or making up a small class for this. One question to consider is: "can you
have multiple string "keys" in your leaf nodes?" This would make the Map option difficult.
You could use something else besides the string/userObject as a key, perhaps a TreePath or
index into the TreeModel. This structure will have to be available to your renderer, ie, the
renderer would need a reference to the class so it could access the data, and maybe to your
TransferHandler.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
I spent sveral days to digest and study your excellent comments and tried several ways to implement it.
I tried to use several maps to catch all useful information and tried to setup some links between these maps, but link lost when I change the name of the node so icons also lost.
I also tried to use TreePath and index (it looks like a coordinates of X axis and Y axis to locate a node)to identify the node but it must be put into TreeSelectionListener, so when node's name is changed, It cannot be caught immediately so that cannot be associated with proper icon.

Actually that will be great if there is one more column for Hashmap other than <key, value> pair, i mean if we can construct a set pair such as
<key1,key2, value> then we can put all original node name into key2 and to use key2 to search and key1 to display.

I tried HashTable, but only <key, value> pair;

In Java, is there any class which has more than two elements pair such as <key1,key2, value> or <key1,value1, value2> etc??

good weekends.

Thanks


But
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Collections like HashMap and HashTable are the data structures that the designers made for
us to use. If they don't suit your needs you can make up something that does. Anything that
does what you want and is easy to read is generally okay.

Edit:
You can make one of these for each leaf node.
[ May 19, 2007: Message edited by: Craig Wood ]
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Thanks so much, I creat this DataStore class, and I can refer to the instance of this class in following:
class PseudoRenderer extends DefaultTreeCellRenderer {
Map iconMap;
DataStore datastore;
public PseudoRenderer(Map iconMap) {
...
// Cast is required for non-generics.
// ImageIcon icon = (ImageIcon)iconMap.get(key);


and replace map by DataStore datastore;
and I use a vector v to store all datastore object that was initialized with each coming DnD component,

when I change the name one node such as "GoldFish", fish icon still gone;
because I am not sure how to associate value with this datastore in following code
protected boolean isFish(Object value) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
Animal nodeInfo = (Animal)(node.getUserObject());
String title = nodeInfo.animalName;
if (title.indexOf("Fish") >= 0) {
return true;
}

here value is a node object, it has one method value.toString(); so once its text string changed, cannot track its original names.
Because I need to change both leaf name and node's name, that is why I use value, not leaf parameter.

Any further idea will welcome
Thanks so much

Michelle
[ May 20, 2007: Message edited by: Michelle Wang ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Make your generics/non–generics selection here:

You can get any LeafNodeStore from LeafStores by its TreePath in the tree and change the
icon as you like.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Excellent code, thanks so much, I tried to edit and change the nodes' names and leaf's name, but cannot, I add tree.editable(true) etc that code works on my part, but here still not work, can we do that??
great thanks.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Try using a triple–click on the node text to begin editing of the node.
End editing by pressing the enter key.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Thanks a lot, small question, If I hope to Drag nad Drop a subtree AA (AA has children of subtree CC, leaves DD1, DD2, DD3 etc) under the main tree into another subtree BB(has children of EE1, EE2 etc), I found the all leaves or children and their icons under this AA(such as children of subtree CC, leaves DD1, DD2, DD3 etc) are lost when AA becomes BB's Child,

and also, when node's name changed from CC to Tiger, Tiger icon is same as CC, if move Tiger leaf from subtree AA to BB, then Tiger's icon gone, anyway can remedy??

I tried to update your code, not success.
Thanks so much!!
[ May 22, 2007: Message edited by: Michelle Wang ]
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I found the all leaves or children and their icons...are lost
The TransferHandler uses a StringTransferable to transfer a string for a single node. So
this transferHandler would probably transfer only the userObject string of the subtree root
node into the tree. To transfer a subtree you will have to make changes to/re–design the
transferHandler.
Michelle Wang
Ranch Hand

Joined: Apr 17, 2007
Posts: 87
Really thants a lot, let me try again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can I change this tree with different Icons for Different nodes at Run Time