Search code examples
javaswinguser-interfacelayout-managergridbaglayout

Create GridBagLayout fixed size buttons


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?


Solution

  • 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.