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?
Joined: Oct 21, 2008
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?
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.
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.
Joined: Oct 21, 2008
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:
subject: JTree row selection by mouseclick question