Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make 2 JButtons Equal in size

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());
like image 288
Kaushik Balasubramanain Avatar asked Sep 13 '11 06:09

Kaushik Balasubramanain


People also ask

How do I make two buttons the same size in CSS?

Answers to the following questions. Select a width and height for your button, and use them for the centre of your text horizontally and vertically : width:120px; height:50px; text-assign-center:1:1. It must be 1em, and it should have ble of 1em; font-size:1.

How do you do the gap between two buttons?

There are several methods that we can use to put spacing between two buttons. But the easiest way to achieve this is by using the margin property. You can either apply margin-right on the first button or margin-left on the second button. Both ways you can achieve the same task.


1 Answers

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

like image 164
aioobe Avatar answered Sep 24 '22 04:09

aioobe