Halfwarr
Halfwarr

Reputation: 8103

Setting JButton text to align to the left?

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?

Upvotes: 21

Views: 42120

Answers (2)

Yanflea
Yanflea

Reputation: 3934

Try

button.setHorizontalAlignment(SwingConstants.LEFT);

Upvotes: 10

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

You need to use

  setHorizontalAlignment(SwingConstants.LEFT)

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

Upvotes: 40

Related Questions