wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTree row selection by mouseclick question 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 "JTree row selection by mouseclick question" Watch "JTree row selection by mouseclick question" New topic
Author

JTree row selection by mouseclick question

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
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?
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
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?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

Have you tried out JTree#getClosestRowForLocation(int x, int y)?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
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.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

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.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
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:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTree row selection by mouseclick question