This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTree row selection by mouseclick question

 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried out JTree#getClosestRowForLocation(int x, int y)?
 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11027
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic