Forums Register Login

JTree row selection by mouseclick question

+Pie Number of slices to send: Send
Is is it possible to select a JTree's row not just by clicking on the name of the row, but by clicking anywhere in the row?
I think the default behaviour(you can select only by clicking on the name) is caused by the default tree node renderer which resizes the JLabel renderer component based on the content. So it won't detect whenever I click next to the node's name.
My first idea is to create a tree node renderer which turns off content based resizing of the renderer component and setting the size of the renderer to fit the width of the tree. Is this the proper way?
+Pie Number of slices to send: Send
It looks I can't implement the required behaviour this way. I tried to get the size of the tree in getTreeCellRendererComponenet, but it returned 0, 0. Even if I can get the size of the JTree I have to determine somehow the position of the JLabel component, and determine the size based on that information. So it looks like this is not the right way. Do anybody have any idea how to achieve the required behaviour?
+Pie Number of slices to send: Send
Have you tried out JTree#getClosestRowForLocation(int x, int y)?
+Pie Number of slices to send: Send
Thanks, that solved half of my problem. The other half is to highlight the whole selected row, not just the text. For this I should force JTree to fit the renderers width instead of using the preferred width. Unfortunatelly I have no idea how to achieve this.
+Pie Number of slices to send: Send
If you can post some short code we can help you figure out the solution.
In a nutshell, show us what have you got so far, and we can take it up from there.
+Pie Number of slices to send: Send
At the moment I have a partial solution, which allows to select a tree node by clicking anywhere in the row(but it won't highlight the whole row). JTree selects a row whenever the user clicks the renderer component. I think highlighting the whole row can be achieved by sizing the renderer somehow to fit the remaining width in the row. This would make the first part unnecesseary since the renderer would fit the whole row.
I tried to size the renderer component in the getTreeCellRendererComponent method, but that did not work. I think JTree resizes the components after I pass it from getTreeCellRendererComponent .
So I'm stuck at this point.

Here's the code of my tree:



And here's the code of the renderer:

Pay attention! Tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 6483 times.
Similar Threads
JCheckBox in Jtree Problem
How to set multiple icons in a TreeCell?
jsf tutorial
how can I change this tree with different Icons for Different nodes at Run Time
JTree with checkboxes not getting updated question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:19:21.