How can I set a component (say button) at center of the panel?
I used Flowlayout
with layout constraint as center, but I am getting button at top-center position of the panel.
Asked
Active
Viewed 8,576 times
0

Andrew Thompson
- 168,117
- 40
- 217
- 433

svkvvenky
- 1,122
- 1
- 15
- 21
3 Answers
10
This can be achieved using either GridBagLayout
as mentioned by AVD1 or BoxLayout
. See this answer for sample code.
Personally I'd use GBL for this because fewer lines of code are required to get the component laid out & on-screen (centered in the parent container).
- I do not understand why that answer is not getting more up-votes, but that aside..

Community
- 1
- 1

Andrew Thompson
- 168,117
- 40
- 217
- 433
-
1+1 for this and this - http://stackoverflow.com/questions/5621338/about-swing-and-jtable/5630271#5630271 – KV Prajapati Feb 01 '12 at 08:24
-
1@AVD It's one of my favorite answers. Especially because of the pretty screen-shots supplied by trashgod (I often borrow them for illustrating points in other threads). Really rounds it out nicely. :) – Andrew Thompson Feb 01 '12 at 08:34
6
Use GridBagLayout
instead of FlowLayout.
JPanel panel=new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(new JButton("Sample")); // will use default value of GridBagConstraints

KV Prajapati
- 93,659
- 19
- 148
- 186
-
1The first 2 lines can be written as one. +1 (one of the few things for which I [use GBL](http://stackoverflow.com/a/5630271/418556).) – Andrew Thompson Feb 01 '12 at 04:10
0
Use a null
layout and set the button's bounds, like this:
// assuming you're extending JPanel
private JButton button; // data field
...
this.setLayout(null);
this.addComponentListener(new ComponentAdapter(){
public void componentResized(ComponentEvent e){
setButtonBounds();
}
});
private void setButtonBounds(){
int bw = 90; // button width
int bh = 30; // button height
int w = getWidth();
int h = getHeight();
button.setBounds((w - bw) / 2, (h - bh) / 2, bw, bh);
}
If you start getting many buttons or a complex layout, consider using MigLayout.

rtheunissen
- 7,347
- 5
- 34
- 65
-
2-1 for even suggesting a `null` layout. Edit that out & I will remove the down-vote (but I am not giving an up-vote for `MigLayout`, when there are 2 J2SE layouts that can do what is required). – Andrew Thompson Feb 01 '12 at 04:22
-
2I've never really understood why `null` layout is so bad, but fair enough. I'll leave my answer and accept the -1. – rtheunissen Feb 01 '12 at 04:26
-
See [setLayout(null) is never necessary. Ever!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374) for my comments (some uncharitable people might call it a 'rant') on the subject. – Andrew Thompson Feb 01 '12 at 04:28
-
Won't start arguing here, but I agree with many of the arguments against banishing `null` layout / absolute positioning. AP definitely has a place in GUI design, and because many solutions to a problem are perfectly valid, they're equally correct if the effect is the same. – rtheunissen Feb 01 '12 at 04:36
-
2Sure, but why not encapsulate that logic into a layout (wherein it becomes unnecessary to call `setLayout(null)`? Even your example above could (both be improved and) be better encapsulated in a layout. It seems to make sense to do it inline in the code until you require a 2nd (3rd ..17th) instance of 'center a component', which is a pretty basic requirement BTW. As to the comment that it can 'be improved'. Why hard-code the widths of the button when there is `getPreferredSize()` (which takes into account the current button text, borders, PLAF,..)? Almost a **classic e.g.** of my point! – Andrew Thompson Feb 01 '12 at 06:12
-
1+1 for making good sense, and I actually do agree with you. It does seem redundant to nullify the layout and set the bounds manually. I think it comes down to a lack of understanding of how the core layout managers actually function. So I agree that a manager / encapsulation is better, but they would still both be right answers and I wouldn't -1 someone for suggesting an appropriate (perhaps silly) use of a `null` layout. Again, I don't think this is the place for this discussion, so I'll end things there. Thanks though. – rtheunissen Feb 01 '12 at 06:24