1

I want to create buttons for a GUI that have 3 different fixed sizes. I want the size of the buttons to be independent of the String inside them. Is that possible with GridBagLayout?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
tur1ng
  • 1,082
  • 1
  • 10
  • 24
  • 1
    `GridLayout` yes, `GridBagLayout` not as far as I understand.. ***unless*** you use a hack to write the strings to same sized images & use the images as icons for the buttons. – Andrew Thompson Jun 25 '16 at 09:44
  • 1
    After considering why you want to do this, please ensure that the resulting buttons are no smaller than their preferred size. – trashgod Jun 25 '16 at 10:22
  • 1
    `3 different fixed sizes.` - define what that means to you. Do you just want to change the Font to make the displayed text easier to read? Do you just want to increase the Border around the button but keep the text the same size? – camickr Jun 25 '16 at 14:53

2 Answers2

2

As an alternative, consider using an available sizeVariant, illustrated here.

image

If you pursue a custom ButtonUI approach, shown in the last row, please ensure that the resulting buttons are no smaller than their preferred size. Doing so in your implementation of getPreferredSize() will help avoid the pitfall seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Try setting the preferredSize on the button(s) to the size you want. Like this:

myButton.setPreferredSize(new Dimension(width, height));

If for some reason that doesn't keep the button's size fixed, try setting the button's maximumSize and minimumSize to the same dimension.

myButton.setMaximumSize(new Dimension(width, height));
myButton.setMinimumSize(new Dimension(width, height));

This would get your button fixed to one size. I'm not sure how you could fix it to multiple sizes, like a small, medium, or large size (based on its text).

Zac
  • 730
  • 1
  • 12
  • 19