Icon position in JButton

Krzysztof Majewski picture Krzysztof Majewski · Jul 26, 2015 · Viewed 7.8k times · Source

Is there any posibility of changing Icon image posintion in JButton?

This is how it looks now:

enter image description here

I want to move the icon more to the left.

I have tried to change the text alignment but it doesn't work as I want:

myButton.setHorizontalTextPosition(SwingConstants.RIGHT);

Answer

Joel Geiser picture Joel Geiser · Jul 26, 2015

Have a look at JButton.setHorizontalAlignment(int align)

Sets the horizontal alignment of the icon and text. AbstractButton's default is SwingConstants.CENTER, but subclasses such as JCheckBox may use a different default.

you can set:

  • SwingConstants.RIGHT
  • SwingConstants.LEFT
  • SwingConstants.CENTER
  • SwingConstants.LEADING
  • SwingConstants.TRAILING