JButton Position Icon

Is there any way to change the position of the icon image in JButton?

Here's what it looks like now:

enter image description here

I want to move the icon more to the left.

I tried changing the alignment of the text, but it does not work the way I want:

myButton.setHorizontalTextPosition(SwingConstants.RIGHT);
+4
source share
3 answers

Decision

I added an empty border (you can add any type of border) to myButton, and now it looks like this:

enter image description here

the code:

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

You can change the space between the icon and the text with setIconTextGap(int). Also, aligning the content to the JButtonleft can help with setHorizontalAlignment(SwingConstants.LEFT).

+3
source

JButton.setHorizontalAlignment(int align)

. DefaultButton - SwingConstants.CENTER, , JCheckBox, .

:

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

Source: https://habr.com/ru/post/1599753/


All Articles