Is it possible to put text on top of a image in a button?

Roman picture Roman · Apr 26, 2010 · Viewed 26.4k times · Source

I have .jpg images in my buttons. I also would like to put some text on top of the images. I use the following syntax for that:

JButton btn = new JButton(label,icon);

But I do not see text in the buttons (only image). What am I doing wrong?

Answer

camickr picture camickr · Apr 26, 2010

I have no idea why you are not seeing the text and icon. By default the text should be painted to the right of the icon.

To display the text on top of the icon you use:

JButton button = new JButton(...);
button.setHorizontalTextPosition(JButton.CENTER);
button.setVerticalTextPosition(JButton.CENTER);