This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing: getting scrollpanel to behave correctly  RSS feed

 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a panel of buttons (ButtonPanel) that uses a FlowLayout and sits inside a ScrollPane. The number of buttons and hence the size of the panel will change between runs of the application. Here's a picture:



The problem I'm having is getting the scrollbars to behave correctly. It's the first time I've tried using these, so it's probably just because I don't know how to use them, although I've read the Sun tutorial.

What I want it to is to only have scrollbars when they're needed, and for the scrollbars to be the correct size.

The scrollbars at the moment seem to be determined by the preferredSize of the panel that the scrollpane is viewing. If I resize the window so that it's taller than the preferred vertical size, the scrollbar disappears:



If I change the panel's vertical preferred size to something huge, the scrollbar is too big, i.e. it lets you scroll down into miles of white space.

How can I set things so that the scrollbars are just the right size to cover the panel it's viewing (the vertical and horizontal sizes of which may change dynamically when the frame is resized, e.g. if the panel becomes wide enough to fit multiple buttons horizontally)?

Here's my code - most of it generated by the GUI builder - the only bit I added were the 4 lines below the initComponents() call.

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't set the perferredSize. Use an appropriate layout and call revalidate() and repaint() any time you add/remove components to/from an already visible container.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl, if I do as suggested and change my code to



I get this:


Whereas I want the buttons to be arranged so that they fit as many across as will fit, and scroll downwards for the rest. I suppose I need to tell the layout manager this somehow, but I don't know how... actually I though that was how FlowLayouts behaved by default.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I said
Use an appropriate layout
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any suggestions?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Wrap Layout should do what you want.
 
Luigi Plinge
Ranch Hand
Posts: 441
IntelliJ IDE Scala Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The Wrap Layout should do what you want.


Brilliant, thanks, it works!

Seems like really bread-and-butter functionality though... amazing that you can't just stick a panel in a scrollpane and have the scrollbars work under standard Java.
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The scrollpane does work properly under standard java.

You want special functionality, which is to "wrap" components on a different line. That is a special layout manager and has nothing to do with the scrollpane.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!