Solved

load an image ......

Posted on 2004-09-22
20
185 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
micro services spring boot application error 3 199
web application structure 18 77
What's wrong with this code? 4 23
sql import cannot be resolved jsp 3 26
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

831 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