Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Help for GridLayout and ScrollPane

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I am currently writing a little program in Swing. And I hit a problem I can't seem to be able to solve alone.
In my program I have a list of objects of unkown size. They are displayed as panels organized in a GridLayout. Each panel take a line of the GridLayout.
I would like, if there are more than 10 objects to show only 10 and have a vertical scroll bar to see all objects.

I encounter 2 problems:
1) I can't seem to be able to show only 10 lines, everything is displayed.
2) I can't seem to have a working scroll bar.

I wrote this code as a simplification of my problem:

Here is the result:


I tried things like setting sizes or preferred sizes for almost everything. I tried to change the number of rows of the GridLayout but nothing helps. I am clearly missing something, I don't know what.

Thanks in advance for your help.
 
Rancher
Posts: 3232
30
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The default layout manager for a JPanel is a FlowLayout which respects the preferred size of any component added to it.

You can't just use the BorderLayout constraint.

If you want a BorderLayout, then you need to set the layout manager to a BorderLayout:




Now the size of the scroll pane will adjust dynamically as the frame is resized and scrollbars will appear as needed.
 
Joss Paz
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much!

I knew the solution had to be simple^^
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic