[Webinar] Streamline your web hosting managementRegister Today

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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

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