Solved

load an image ......

Posted on 2004-09-22
20
168 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
  • 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 77
countHi challenge 25 84
Updating Java 9 87
base64 decode encode 12 96
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now