JButton icon problems

P

Phill

Hi guys.

I'm having a problem setting the icon on JButtons. Basically I have a 2d
array of ImageIcons and depending on certain criteria I want to set the icon
displayed on the JButton.

At the start of the program I load all the images (gif files) into the array
using:

cardImages[0] = new javax.swing.ImageIcon("/"+(i+2)+"h.gif");

and then to set the icon i'm using,

myButton.setIcon(cardImages[indexB][indexA]);

I'm not getting any error messages, but the images just won't display on the
buttons.

Anyone got any ideas?
 
J

John L. Webber

Phill said:
Hi guys.

I'm having a problem setting the icon on JButtons. Basically I have a 2d
array of ImageIcons and depending on certain criteria I want to set the icon
displayed on the JButton.

At the start of the program I load all the images (gif files) into the array
using:

cardImages[0] = new javax.swing.ImageIcon("/"+(i+2)+"h.gif");

and then to set the icon i'm using,

myButton.setIcon(cardImages[indexB][indexA]);

I'm not getting any error messages, but the images just won't display on the
buttons.

Anyone got any ideas?


You may need to load each image with a java.awt.MediaTracker first.
 
H

Harald Hein

John L. Webber said:
Phill said:
cardImages[0] = new javax.swing.ImageIcon("/"+(i+2)+"h.gif");

You may need to load each image with a java.awt.MediaTracker first.


No, he uses ImageIcon, which does this. My guess is that the
constructed file names are wrong. Note the absolute path name beginning
with "/".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top