Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

load an image ......

Posted on 2004-09-22
20
Medium Priority
?
205 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
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 600 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 600 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

824 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