This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting JTree Row Components  RSS feed

 
Chris Heady
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was having an interesting problem with a JTree. I want each JLabel within a row (my TreeCellRenderer extends a JLabel) to extend to the edge of the JTree. Which means having every row be the same size as the largest JLabel in the JTree. This sounds easy, but I can't just set the size of the JLabel to the JTree size in the CellRenderer, because we haven't build the entire JTree yet, it is just getting each component individually. We also don't know how big the biggest label is because again we have not build the entire JTree yet. I guess I could do two passes at the rendering where I save the max size on the first creation of the JTree, and then recreate based on that max number. But that seems hacky, nevermind the performance issues.
The one way I thought to do this was to set each row within the JTree to have the parent container of each JLabel have a BorderLayout of Center so that the JLabel will take up the entire row it was given. I cannot do this in the TreeCellRenderer as all I have access to is the JLabel and not its parent component that has control over the layout. So I figure that the JTree has control over each rows container (looks like the container is a JList). However when I look into JTree's source, I cannot find where it lays out each row. All I can find is getCurrentComponent, which is used by the accessibility inner class. Does anyone know what class I need to subclass so that I may change the layout of rows? Thanks for any help.
[ March 05, 2002: Message edited by: Chris Heady ]
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before I ramble on let me say that I am not a Swing expert so you may want to take this with a grain of salt. But here goes.
First why not subclass DefaultTreeCellRenderer which already subclasses JLabel and is tuned for performance?
Inside your subclass you could keep a static reference to the largest JLabel and when your JTree was complete, call a method on your TreeCellRenderer which fires a property change that indicates that the JTree should resize itself.
Of course this isn't a much better "hack" than the one you described but with JTree you don't have a lot of options ie Scrollable and Accessible interfaces
Hope this helps
 
Chris Heady
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply,
My treeCellRenderer already extends the DefaultTreeCellRenderer. The problem with the resizing the tree idea after all the renderers have been fired and saved the max is that the components (JLabels) within the JTree do not get resized when the JTree is resized. I want to resize all of the JLabels within the JTree so that the labels take up the same space horizontally as the JTree.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!