Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jtree Scrolling problem  RSS feed

 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to expand the Jtree, I'm not getting the vertical scrollbar. What should I do?
this is my code,
jPanel1.add(jTree1);
getContentPane().add(new JScrollPane(jPanel1));
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
change your code to:
jPanel1.add(new JScrollPane(jTree1));
getContentPane().add(jPanel1);
chantal
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting the scrollbar for jtree. but i'm unable to set the width for the tree. here is my code,
//treePanel.setPreferredSize(new java.awt.Dimension(100,400));
treePanel.setAutoscrolls(true);

treePanel.add(mibTree);

getContentPane().add(new javax.swing.JScrollPane(treePanel), java.awt.BorderLayout.WEST);
if i remove that comment, i'm getting the width i want. but i'm losing the scrollbar.
how do i do this.
 
Don Bosco
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys, I need help on the above problem. any help is greatly appreciated.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By setting preferred size on the panel you are also restricting how big it can become... this isn't what you want to do.

Just add the tree as the main component in the scroll pane, not a panel holding the tree. I'm not clear on why you are trying to specify a width... if you just add the tree to the scroll pane, the scrollpane automatically knows the largest width to let you scroll to be able to see the tree. If you are trying to make the scrollpane wider, set the preferred size on the scroll pane instead of the tree...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!