Solved

load an image ......

Posted on 2004-09-22
20
178 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 73
Java - sorting a list of objects where the properties of the objects can change during the sort 7 47
Java Timer (static) 9 36
egit plugin on eclipse 8 41
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

863 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

23 Experts available now in Live!

Get 1:1 Help Now