How can I make a button exactly the same size of its text?

PB_MLT picture PB_MLT · Apr 26, 2010 · Viewed 9.7k times · Source

Is it possible to make a JButton take exactly the size of its text? Since by default, a JButton will have a small amount of padding (both horizontal and vertical) around its text. I would like to remove that padding.

Answer

Peter Lang picture Peter Lang · Apr 26, 2010

JButton has a border by default, you can remove it:

button.setBorder(null);

If you want to keep the border, reduce the margin (see Eugenes answer)