• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JTree row selection by mouseclick question

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic