1

I have a BoxLayout panel that has some components from top to bottom and I want this to be centered (both vertically and horizontally) in the content pane, so that when I maximize the window this inner panel will still remain at its center.

I've managed to do this with BoxLayout and/or GridBagLayout, but since I'm experimenting with Swing I was wondering if I can do the same by using FlowLayout or BorderLayout as the content pane's layout manager. I couldn't find a way to do it... When I maximize the frame the inner panel remains centered horizontally but not vertically (it's stuck at the top).

Can it be done with these 2 layout managers or I'm wasting my time?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rempelos
  • 1,220
  • 10
  • 18
  • the latter :-) Each layoutManager has a different behaviour which you need to learn and then choose which is best for your context. The api doc of all core managers is ... scarce, the tutorial linked to in the swing tag has some more details. – kleopatra Jul 18 '12 at 15:44
  • @kleopatra: that's clear, thanks – Rempelos Jul 18 '12 at 17:45

1 Answers1

1

FlowLayout starts it component positions at the top of the component. You can change the horizontal anchor. BorderLayout has 5 positions you can use to layout components, a center position that will occupy the max amount of free space, & 4 positions around the outside edge.

By placing a component in the center position, you can effectively be centered around the vertical & horizontal position of he continent, but the component will be resized to fill the remaing space

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • what's a "horizontal anchor"? – kleopatra Jul 18 '12 at 15:36
  • 1
    Flow layout lets you pass in an alignment value of SwingConstants.LEFT, SwingConstants.RIGHT or SwingConstants.CENTER – MadProgrammer Jul 18 '12 at 20:00
  • when you are talking abount alignment, then call it ... well ... alignment :-) Inventing terms isn't soo good in technical communication, IMO – kleopatra Jul 19 '12 at 09:08
  • Alignment anchors the components, sorry, that's just how I think of it, comes from many years of using the gridbaglayout, which uses anchors to align its components – MadProgrammer Jul 19 '12 at 10:01