Make 2 JButtons Equal in size
Asked Answered
P

3

6

I have two JButtons with texts "Ok" and "Cancel". I am using GridBagLayout to align them in a JDialog. I have set the anchor to GridBagConstraints.CENTER. Due to the difference in the number of characters in the texts "Ok" and "Cancel", the buttons are of different sizes. How do I align them correctly so that each of them have the same size. I tried the following but no avail.

okayButton.setSize(cancelButton.getSize());
Pappano answered 13/9, 2011 at 6:13 Comment(1)
just for emphasis: sizing/positioning the components is the job of the LayoutManager (which you already use, good!) - setSize in application code has (and is expected to and must have :-) no effectNolannolana
B
1

Try setting the fill to GridBagConstraints.BOTH and give both buttons equal weight.

Bait answered 13/9, 2011 at 6:16 Comment(0)
H
1

GridBaglayout have got GridBagConstraints and in all cases accepts PreferredSize

examples here and here

Hyperbole answered 13/9, 2011 at 6:19 Comment(0)
B
1

Instead of okayButton.setSize(cancelButton.getSize()); use okayButton.setPreferredSize(cancelButton.getPreferredSize());

Bitthia answered 13/9, 2011 at 6:56 Comment(2)
-1 no ... never-ever use setXXSize in application code (for some reasons, see #7229726) instead use a decent LayoutManagerNolannolana
By the way, should I remove answers those may mislead people?Bitthia

© 2022 - 2024 — McMap. All rights reserved.