I've never really looked into layouts before, and it seems very confusing to me with all the choices given. What I'm trying to do is position buttons in one column down the centre of my JPanel (and maybe in one place have 2 buttons in one row). My question is; which layout manager should I use?
Asked
Active
Viewed 318 times
0
-
1Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Jan 04 '14 at 10:13
2 Answers
4
What about BoxLayout
with vertical orientation or GridLayout
with one column and multiple rows?
To have 2 buttons in one row place a sub panel with the 2 buttons the same way as simple buttons.

StanislavL
- 56,971
- 9
- 68
- 98
-
1The 'in the center' implies to me a `Gridlayout`/`BoxLyout` **inside a `GridBagLayout`** +1 for your current suggestion though. An example of centering a component can be seen in [this answer](http://stackoverflow.com/a/7181197/418556). – Andrew Thompson Jan 04 '14 at 10:10
-
1@AndrewThompson I agree that additional layout manager required. It could be even FlowLayout for the simplest case. – StanislavL Jan 04 '14 at 10:15
-
Yes, I was just looking more closely at the question and noticed the .. *"and maybe in one place have 2 buttons in one row"*. `FlowLayout` would be excellent for that part. – Andrew Thompson Jan 04 '14 at 10:16
0
You can do it with just a BoxLayout, then centring the components inside the layout.
pnlMain.setLayout(new BoxLayout(pnlMain, BoxLayout.Y_AXIS)); //Creates the layout
And then in a component's class:
this.setAlignmentX(Component.CENTER_ALIGNMENT); //Centres the button

Jake Stanger
- 449
- 1
- 8
- 24