Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

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);

Upvotes: 4

Views: 10998

Answers (3)

Krzysztof Majewski
Krzysztof Majewski

Reputation: 2534

SOLUTION

I've added an empty border(you can add any type of border) to the myButton and now it look like that:

enter image description here

Code:

myButton.setBorder(BorderFactory.createEmptyBorder(4, 4, 2, 20));

Upvotes: 2

NESPowerGlove
NESPowerGlove

Reputation: 5496

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

Upvotes: 3

JoGe
JoGe

Reputation: 910

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

Upvotes: 3

Related Questions