21

I prefer buttons with minimal margins, about as wide as their text caption. Is there a way to achieve that in a JButton in Swing?

What I am seeing in practice now is that even if I try to use setMaximumSize() and similar, it just ends up eating the text caption, cutting it off to the right. But it does not reduce the margins.

niton
  • 8,771
  • 21
  • 32
  • 52
EndangeringSpecies
  • 1,564
  • 1
  • 17
  • 39

2 Answers2

48

I believe setMargin is what you're looking for.

myButton.setMargin(new Insets(0, 0, 0, 0));
Michael Myers
  • 188,989
  • 46
  • 291
  • 292
1

Worth noting you can make the values for Insets -ve (EDIT: read negative). This makes things even more compact and if done carefully doesn't look unpleasant.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
UnixNerd
  • 333
  • 1
  • 4
  • 15