Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

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 !
0
meow00
Asked:
meow00
  • 7
  • 6
  • 2
  • +4
2 Solutions
 
girionisCommented:
Are you sure it can find the "bonb.gif" file?
0
 
zzynxSoftware engineerCommented:
Make sure your path is correct.
Try with a full path just to test it works
0
 
zzynxSoftware engineerCommented:
>>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"))) ;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
girionisCommented:
> Are you sure it can find the "bonb.gif" file?

I mean "bomb.gif" file of course.
0
 
meow00Author Commented:
Hi zzynx & experts,

   I tried the full path, but it says :

   java.awt.image is abstract, can not be instantiated ........
0
 
zzynxSoftware engineerCommented:
Try

       ((Mine)e.getSource()).setIcon(new ImageIcon("c:/dir1/dir2/bomb.gif"));
0
 
girionisCommented:
Try this:

((Mine)e.getSource()).setIcon(new ImageIcon(c:/dir1/dir2/bomb.gif")) ;
0
 
zzynxSoftware engineerCommented:
Or

     ((Mine)e.getSource()).setIcon(new ImageIcon("bomb.gif"));
0
 
girionisCommented:
>    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?
0
 
meow00Author Commented:
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 !
0
 
girionisCommented:
> 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.
0
 
Giant2Commented:
>((Mine)e.getSource()) is a button object
the method setIcon is for JButton object.
0
 
objectsCommented:
can you post the location of your image
the location you are running it from
and the location of your class file
0
 
Giant2Commented:
>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.
0
 
zzynxSoftware engineerCommented:
Where do you call that

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

?
Is it in an ActionListener, MouseListener,...
What are you trying to do?
0
 
RuadRauFlessaCommented:
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.
0
 
funnyveryfunnyCommented:
how about using double slashes rather than one?

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 6
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now