20

I previously had a JLabel, that I wanted to be click-able. The easiest way I found to do this was make it a JButton and using the following code. It now looks like a JLabel

button.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.setFocusPainted(false);

Which is exactly what I wanted except the text is now aligned in the middle. Now from what I was able to read on other questions and searching. This should work

button.setHorizontalTextPosition( SwingConstants.LEFT );

Yet, the text still aligns in the middle of the button. Any ideas what I can do to change this?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Halfwarr
  • 7,853
  • 6
  • 33
  • 51

2 Answers2

38

You need to use

  setHorizontalAlignment(SwingConstants.LEFT)

HorizontalTextPosition refers to the position of text in relation to the icon.

Vincent Ramdhanie
  • 102,349
  • 23
  • 137
  • 192
10

Try

button.setHorizontalAlignment(SwingConstants.LEFT);
animuson
  • 53,861
  • 28
  • 137
  • 147
Yanflea
  • 3,876
  • 1
  • 14
  • 14