JTree - How to detect user clicked the tree image icon

How does one detect a user clicked on the tree image icon node only?

If add TreeSelectionListener to solve this problem, that doesnt work because you can select the text part of the tree node and dont want that.

In my situation, the user has updated the checkboxes in the JTable tree child node, it then puts a disk image on the editing tree node. The editing tree node image changes from the folder image to a disk save image. The idea is, when the user clicks on the disk image of the tree node, it will save the data. There must be a practical way of implementing such that clicking on a tree image node only, will perform a particular operation. Please provide source sample to solve this

[713 byte] By [akhalil100a] at [2007-11-27 7:55:25]
# 1

> Please provide source sample to

> solve this

You'll have to do it yourself, but here is one way to do this - create a custom renderer that has two labels, one for icon and one for text. Than, in the tree listener get the renderer for that row / path, and get the bounds of the icon label. If the mouse click is in that bounds, the user clicked on the icon.

kirillga at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 2
There has to be a generic way of doing this, since tree icon image size are 16x16, or whatever size it was rendered at. Please provide source code
akhalil100a at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 3
> Please provide source codeif you don't want to (or can't) write it yourself, pay someone to do it. http://www.rentacoder.com/RentACoder/default.asp
Michael_Dunna at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 4
If I dwell on the last comment than ignore the things mention Ive done so far in the introduction. Beside, there is a problem and mention about source code in the introduction. Dont reply if cant provide the help, simple
akhalil100a at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 5

The following is a copy out the original sun java documentation (JavaTM 2 SDK, Standard Edition, Version 1.4.2l) for JTree:

...

If you are interested in detecting either double-click events or when a user clicks on a node, regardless of whether or not it was selected, we recommend you do the following:

final JTree tree = ...;

MouseListener ml = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

int selRow = tree.getRowForLocation(e.getX(), e.getY());

TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());

if(selRow != -1) {

if(e.getClickCount() == 1) {

mySingleClick(selRow, selPath);

}

else if(e.getClickCount() == 2) {

myDoubleClick(selRow, selPath);

}

}

}

};

tree.addMouseListener(ml);

DeutscherMichela at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Dont reply if cant provide the help, simple

the part requesting help - you used invisible font for that?

you were told how to do it, but no, not good enough, you want it served to you on a platter:

"Please provide source sample to solve this"

"Please provide source code"

Perhaps you should return to page 1 of the beginners book and learn how to

write code, instead of using everyone else's code.

Michael_Dunna at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 7

Do you think this is an easy problem to solve? To detect click on a tree node is simple. To add mouse listener to detect treepath is simple, but to detect you select on a "tree image node" is not so simple. Ive got one answer which will supply 2 duke dollars (doesnt work now), on the theory to do this but no code.

Another person gave me code solution but that I already know how to do but doesnt answer the question, but give him 1 duke for his try effort. As for the person who thinks this is a beginners guide solution, read carefully what I want. Most softwares dont work because solutions given to requirements not asked for

Abe

akhalil100a at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 8
DeutscherMichel solution is the right track but need the other half solution, the hard part. Why im asking the critical question, how to detect tree image icon clicks. Note again, want to perform operation only when detect only mouse clicks on the "tree image icon nodes"
akhalil100a at 2007-7-12 19:36:47 > top of Java-index,Desktop,Core GUI APIs...
# 9
Sorry about the two many words "only" in the last setence, hehe. Nway that person trying to get on my nerves, please dont reply. Watch another forum :)
akhalil100a at 2007-7-12 19:36:48 > top of Java-index,Desktop,Core GUI APIs...
# 10

> Nway that person trying to get on my

> nerves, please dont reply. Watch another forum :)

At more than 400 forum posts, you should try to relax and respect people that have 50 times as many dukes as you have. You were given a suggestion to solve your problem. People are here on their free time, and you're not paying anyone, so don't expect a complete solution. And anyways, who cares about these dukes, that you're trying to "distribute" among the answers (2 here, 1 here, ...)?

kirillga at 2007-7-12 19:36:48 > top of Java-index,Desktop,Core GUI APIs...
# 11

akhalil100 ,

To detect a tree selection implement a tree selection listener and register it on the tree:

JTree.addTreeSelectionListener();

Or implement a mouse/mouseMotion listener to react on mouse events.

To distinguish between a single or a double mouseclick see the code I posted first.

To recognize the leave that caused the event do the following:

<eventCausingObject> = ((JTree)(<event>.getSource())).getSelectionPath().getLastPathComponent();

To make the leave behave as you want it (icon, text) see class DefaultTreeCellRenderer and interface TreeCellRenderer

I have been given help out of this forum so I want to help others, too, when I'm able to do.

So, let's stop discussing and care about the java problem.

Michel

DeutscherMichela at 2007-7-12 19:36:48 > top of Java-index,Desktop,Core GUI APIs...