Make 2 JButtons equal in size

I have two JButton with the texts "Ok" and "Cancel". I use GridBagLayout to align them in JDialog . I set the anchor on GridBagConstraints.CENTER . Due to the difference in the number of characters in the "OK" and "Cancel" texts, the buttons have different sizes. How to align them correctly so that each of them has the same size. I tried the following but did not help.

 okayButton.setSize(cancelButton.getSize()); 
+6
source share
3 answers

Try setting the fill to GridBagConstraints.BOTH and give the buttons a volume of equal weight.

+1
source

GridBaglayout received GridBagConstraints and in all cases accept PreferredSize

Examples here and here

+1
source

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

+1
source

Source: https://habr.com/ru/post/897201/


All Articles