How do I position text in the middle of an Icon (loaded from a GIF) in a JButton?

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!");
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact