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()).setI con(new ImageIcon(new Image("bomb.gif"))) ;
((Mine)e.getSource()) is a button object .....
thanks !
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()).setI
((Mine)e.getSource()) is a button object .....
thanks !
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
>>Try with a full path just to test it works
I mean
((Mine)e.getSource()).setI con(new ImageIcon(new Image("c:/dir1/dir2/bomb.g if"))) ;
I mean
((Mine)e.getSource()).setI
> Are you sure it can find the "bonb.gif" file?
I mean "bomb.gif" file of course.
I mean "bomb.gif" file of course.
ASKER
Hi zzynx & experts,
I tried the full path, but it says :
java.awt.image is abstract, can not be instantiated ........
I tried the full path, but it says :
java.awt.image is abstract, can not be instantiated ........
Try
((Mine)e.getSource()).setI con(new ImageIcon("c:/dir1/dir2/bo mb.gif"));
((Mine)e.getSource()).setI
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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?
>
> java.awt.image is abstract, can not be instantiated ........
I infer that the problem was that it couldn't find the image file before?
ASKER
Hi zzynx & girionis & experts,
I tried ((Mine)e.getSource()).setI con(new ImageIcon("bomb.gif"));
and ((Mine)e.getSource()).setI con(new ImageIcon(c:/dir1/dir2/bom b.gif")) ; they both compiled. however, there is no image shown on the button ....... why is that ? please help ! Thanks !
I tried ((Mine)e.getSource()).setI
and ((Mine)e.getSource()).setI
> 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.
> 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.
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
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.
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()).setI con(....)
?
Is it in an ActionListener, MouseListener,...
What are you trying to do?
((Mine)e.getSource()).setI
?
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.
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()).setI con(new ImageIcon("C://dir1//dir2/ /bomb.gif" )) ;
((Mine)e.getSource()).setI
>> how about using double slashes rather than one?
Not needed. That's for BACKslashes.
Not needed. That's for BACKslashes.
:)
Thanks