Rempelos
Rempelos

Reputation: 1220

Center component with FlowLayout or BorderLayout (and remain centered after resizing)

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?

Upvotes: 1

Views: 2212

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

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

Upvotes: 1

Related Questions