Solved

load an image ......

Posted on 2004-09-22
20
201 Views
Last Modified: 2010-03-31
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
Comment
Question by:meow00
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +4
20 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 12120704
Are you sure it can find the "bonb.gif" file?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12120706
Make sure your path is correct.
Try with a full path just to test it works
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12120714
>>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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:girionis
ID: 12120744
> Are you sure it can find the "bonb.gif" file?

I mean "bomb.gif" file of course.
0
 
LVL 1

Author Comment

by:meow00
ID: 12120759
Hi zzynx & experts,

   I tried the full path, but it says :

   java.awt.image is abstract, can not be instantiated ........
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12120780
Try

       ((Mine)e.getSource()).setIcon(new ImageIcon("c:/dir1/dir2/bomb.gif"));
0
 
LVL 35

Accepted Solution

by:
girionis earned 150 total points
ID: 12120783
Try this:

((Mine)e.getSource()).setIcon(new ImageIcon(c:/dir1/dir2/bomb.gif")) ;
0
 
LVL 37

Assisted Solution

by:zzynx
zzynx earned 150 total points
ID: 12120787
Or

     ((Mine)e.getSource()).setIcon(new ImageIcon("bomb.gif"));
0
 
LVL 35

Expert Comment

by:girionis
ID: 12120792
>    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
 
LVL 1

Author Comment

by:meow00
ID: 12120855
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
 
LVL 35

Expert Comment

by:girionis
ID: 12120880
> 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
 
LVL 12

Expert Comment

by:Giant2
ID: 12120922
>((Mine)e.getSource()) is a button object
the method setIcon is for JButton object.
0
 
LVL 92

Expert Comment

by:objects
ID: 12120923
can you post the location of your image
the location you are running it from
and the location of your class file
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12120936
>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
 
LVL 37

Expert Comment

by:zzynx
ID: 12120972
Where do you call that

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

?
Is it in an ActionListener, MouseListener,...
What are you trying to do?
0
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12123384
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
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12127573
how about using double slashes rather than one?

((Mine)e.getSource()).setIcon(new ImageIcon("C://dir1//dir2//bomb.gif")) ;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12130596
>> how about using double slashes rather than one?
Not needed. That's for BACKslashes.
0
 
LVL 35

Expert Comment

by:girionis
ID: 12140510
:)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12140515
Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question