Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Icon position in JButton

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);
like image 266
Krzysztof Majewski Avatar asked Feb 09 '23 11:02

Krzysztof Majewski


2 Answers

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
like image 127
JoGe Avatar answered Feb 24 '23 16:02

JoGe


You can change the gap of the space between the icon and text with setIconTextGap(int). In addition, aligning the contents of the JButton towards the left may help with setHorizontalAlignment(SwingConstants.LEFT).

like image 22
NESPowerGlove Avatar answered Feb 24 '23 15:02

NESPowerGlove