• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LayoutManagers and containers

 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi people,

I'm kind of new when it comes to designing a GUI. I find it REAL hard to actually place components where i'd like them. Is the use of LayoutManagers and various containers common in normal Swing GUI?

For example, would i divide the JFrame up into four containers (JPanels):
____
|_||_|
|_||_| (one JFrame, 4 JPanels)

and then each panel can get a diferrent LayoutManager implementation?

Would this be normal? (I can only use the Java Swing API and no other 3rd party project because it's for my SCJD).

And what i don't understand is why the components fill up all of the cells.


Can someone please help me with this design problem i am having?

Thanks and regards,
Marzo.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it normal to use LayoutManagers and Containers? Yes, of course. The only alternative is to use a "null" LayoutManager and place components at absolute coordinates. That's a terrible idea because just resizing a window breaks it, not to mention how it will break as the GUI is run on different platforms.

How to divide a window into 4 panels with their own LayoutManager? Easy. Set the LayoutManager of the JFrame's content pane to a GridLayout or GridbagLayout, and add four JPanels to that grid. GridLayout is simpler, but GridBagLayout would let the four panel's size vary.

Then you can independently set the LayoutManager of each of the JPanels; they can all be different.

Why do your components "take up the whole panel?" Well, each LayoutManager has its own strategy for laying out components; some of these strategies do, indeed, involve expanding components to fill available space. You need to read the Javadoc for each LayoutManager you use, carefully. JFrames have a BorderLayout by default; JPanels have a FlowLayout by default.

Have a look at the often-neglected Box class and the BoxLayout, which is a sort of improved FlowLayout.
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ernest.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic