This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

2 JLists and buttons in a frame which fill available space

 
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks

So I want a couple of JLists left and right with buttons in the middle.

One button moves selected items in the left JList to the right, and the other button works the other way.

The buttons are fixed size, and I want the 2 JLists to fill the remaining space such that they are the same size.

I assume I'm right in using the BorderLayout and positioning the components in its LINE_START, CENTER and LINE_END positions. But how do I go about getting the JLists to fill the left and right spaces within the frame, resizing them as the user resizes the frame?

Some general pointers as to what I should be looking up to get me started would be much appreciated.

Cheers
John
 
Rancher
Posts: 3062
23
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The buttons are fixed size, and I want the 2 JLists to fill the remaining space such that they are the same size.



No, you don't want to use a BorderLayout. The BorderLayout does the opposite of what you want. The CENTER area grows and the LINE_START and LINE_END are fixed size based on the data in the JLists.

The easiest layout manager in the JDK to use would be the GridBagLayout. The GridBagLayout will display components at their preferred size and then you can specify constraints that allow the components to grow/shrink as the frame size changes.

So first you must make sure the preferred size of each JList is the same:



Now you need to specify constraints when you add the JScrollPanes containing the JLists to the GridBagLayout. You will need to use the "weightx" constraint to allow each component to share the extra space. Read the section from the Swing tutorial on How to Use GridBagLayout for more information about the constraints and some working examples to play with.

The other option is to use the Relative Layout which is a 3rd party layout manager. It does support the concept of two components being the same relative size. It is easier to use than the GridBagLayout. The basic code would be:


Now the button panel in the center will be fixed at its preferred size and the two scrollpanes will each take 50% of the remaining space in the frame.


 
John Matthews
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks (again) Rob - I've got the GridBagLayout working nicely, and now using it in other places in my code.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!