Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Swing: getting scrollpanel to behave correctly

 
Ranch Hand
Posts: 441
Scala IntelliJ IDE 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.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
Scala IntelliJ IDE 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
Netbeans IDE Opera Java
  • 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
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
any suggestions?
 
Rancher
Posts: 3205
29
  • 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
Scala IntelliJ IDE 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
Rancher
Posts: 3205
29
  • 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.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic