How do I position text in the middle of an Icon (loaded from a GIF) in a JButton?
Created May 4, 2012
Nicola Ken Barozzi
Let's say that your Gif file filename is
in the String variable myGifFile.
Here is the code:
JButton button = new JButton();
// Here you tell the button to center the text in the horizontal direction
// The text position is relative to the icon. Normally it's RIGHT
button.setHorizontalTextPosition(SwingConstants.CENTER);
// Here you tell the button to center the text in the vertical direction
// (Default is CENTER, so this isn't really necessary)
button.setVerticalTextPosition(SwingConstants.CENTER);
button.setIcon(new ImageIcon(myGifFile));
button.setText("My centered text!");