Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

load an image ......

Hi experts,

   How do I set an image for a button ? setImage doesn't seem to work ... and the following line doesn't work either ... what did I do wrong ?

  ((Mine)e.getSource()).setIcon(new ImageIcon(new Image("bomb.gif"))) ;

   ((Mine)e.getSource()) is a button object .....

 thanks !
Avatar of girionis
girionis
Flag of Greece image

Are you sure it can find the "bonb.gif" file?
Make sure your path is correct.
Try with a full path just to test it works
>>Try with a full path just to test it works
I mean

       ((Mine)e.getSource()).setIcon(new ImageIcon(new Image("c:/dir1/dir2/bomb.gif"))) ;
> Are you sure it can find the "bonb.gif" file?

I mean "bomb.gif" file of course.
Avatar of meow00
meow00

ASKER

Hi zzynx & experts,

   I tried the full path, but it says :

   java.awt.image is abstract, can not be instantiated ........
Try

       ((Mine)e.getSource()).setIcon(new ImageIcon("c:/dir1/dir2/bomb.gif"));
ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>    I tried the full path, but it says :

>    java.awt.image is abstract, can not be instantiated ........

I infer that the problem was that it couldn't find the image file before?
Avatar of meow00

ASKER

Hi zzynx & girionis & experts,

   I  tried ((Mine)e.getSource()).setIcon(new ImageIcon("bomb.gif"));
   and ((Mine)e.getSource()).setIcon(new ImageIcon(c:/dir1/dir2/bomb.gif")) ; they both compiled. however, there is no image shown on the button ....... why is that ? please help ! Thanks !
> however, there is no image shown on the button ....... why is that ?
> please help ! Thanks !

Does it shwo any error messages? Any exceptions? Where do you have the image (full path please)? Do you have it inside dir1/dir2? You have to give the full path, the actual path that correpsonds to the image, or if it is in the same folder as your java class then the first line is correct.
>((Mine)e.getSource()) is a button object
the method setIcon is for JButton object.
can you post the location of your image
the location you are running it from
and the location of your class file
>e.getSource(),
so you use to set the icon in an event handling, isn't it?
If there is no exception, It could be that the repainting isn't done.
Bye, Giant.
Where do you call that

        ((Mine)e.getSource()).setIcon(....)

?
Is it in an ActionListener, MouseListener,...
What are you trying to do?
Trying to change the image on a button after an event occurs it would seem but of course we cannot assume that so.....

Looking at that I would say that the object "e" is of type Event and the class "Mine" is an extension upon the JButton class. I think you should look into the "Mine" class a little bit and make sure that you are doing everythin correctly there. If you by any chance override the setIcon(...) method then you should start looking there.

If what you are trying to do is create mouse over effects there are standard routines built into the JButton that would accomplish that.
how about using double slashes rather than one?

((Mine)e.getSource()).setIcon(new ImageIcon("C://dir1//dir2//bomb.gif")) ;
>> how about using double slashes rather than one?
Not needed. That's for BACKslashes.
Thanks