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

Changing component inside a JScrollPane

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a JScrollPane with the Horizontal ScrollBar set to come on as needed.

I want to be able to remove the component (JPanel) added to scrollpane, and then add a different one (say a larger one),
and have the scrollbar update itself so I can then view all of the new component, but I'm not sure how to do it other than perhaps just rebuilding a new ScrollPane, and I guess I'll have to add this into the JPanel its in again too.

Any thoughts? Thanks
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JScrollPane.setViewportView for changing the component, then call revalidate() and repaint() to enforce that the scroll bars are reset correctly.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I got it to work using:



I'm not sure if thats what you meant.

It also appears to work using either of setViewPortView or revalidate, so I'm not sure.
I guess I'll stick with what I've got above.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly what I meant. revalidate() and repaint() may not be needed, but sometimes you do need them when you modify your user interface at runtime so I always add them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic