> I need to find out the mouse coordinates within a JTree's item. I use my own
> ItemRenderer.
> The aim is to draw a line above, below or through the item on which the
> mouse cursor actually rests.
Will adding a mouse listener to the tree not work?
On composite components, for instance JComboBox under most PL&Fs, it's
sometimes necessary to add the mouse listener to child components as
well (although sometimes that can cause problems too).
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Rene Ruppert - 23 Aug 2005 14:43 GMT
> Will adding a mouse listener to the tree not work?
I thought of that but adding a mouse listener to each and every item doesn't
sound like a good idea to me.
If only I could get the location of an item while rendering it, but
getLocation() always returns the size of the component to be rendered with
negative sign...strange behaviour though.
René
Thomas Hawtin - 23 Aug 2005 16:54 GMT
>[Tom Hawtin (me) wrote:]
>>Will adding a mouse listener to the tree not work?
[quoted text clipped - 4 lines]
> getLocation() always returns the size of the component to be rendered with
> negative sign...strange behaviour though.
There isn't a component for each item. Instead one component, which is
in someways disconnected from the JTree, is used to paint each item in
turn during JTree.paint. The renderer is called to construct a
component. That component is then reshaped to fit the item before painting.
Alternatively you could use a glass pane, but that interferes and is
awkward to forward the mouse event on. Or use
Toolkit.addAWTEventListener, but that requires security permissions.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Bidek - 23 Aug 2005 14:44 GMT
Would this work for you??
private void initSelectionModel() {
structTableTree.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseReleased(java.awt.event.MouseEvent evt) {
// if (evt.getModifiers() ==
java.awt.event.MouseEvent.BUTTON1_MASK)
if (evt.getModifiers() == java.awt.event.MouseEvent.BUTTON3_MASK) {
final javax.swing.tree.TreePath selectedPath =
structTableTree.tree.getClosestPathForLocation(evt.getX(), evt.getY());
structTableTree.tree.setSelectionPath(selectedPath);
structPopupMenu.show(structTableTree, evt.getX(), evt.getY());
}
}
});
}
Rene Ruppert - 23 Aug 2005 15:20 GMT
> Would this work for you??
I'm using something similar right now. But it doesn't solve my problem: i
want to draw a line above the current if the cursor is in the top third, the
middle if the cursor is in the middle third and at the bottom if the cursor
is in the bottom third of the item under the mouse cursor...
Thanks anyway.
René
Babu Kalakrishnan - 23 Aug 2005 19:46 GMT
>>Would this work for you??
>
[quoted text clipped - 6 lines]
>
> René
> Hi,
>
[quoted text clipped - 8 lines]
>
> René
A listener registered on the JTree will give you the coordinates with
respect to the entire JTree component, Now call getClosestRowForLocation
to get the corresponding JTable row, and getRowBounds with the returned
value to find the actual bounds for your row.
Once you have the bounds for the row in question, it should be easy to
correlate the actual mouse location with respect to that.
BK