I have two JButton
s 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());
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.
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.
Try setting the fill to GridBagConstraints.BOTH
and give both buttons equal weight.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With