Solved

How to use ImageIcon

Posted on 1999-01-14
8
548 Views
Last Modified: 2012-05-04
I have the following code


    public void actionPerformed(ActionEvent event) {
        try {
        if (en) {
                String file1 = "file:///c:/jdk1.2/sources/dk/des/lal/test1.jpg";

            URL url = new URL(file1);
            ImageIcon icon = new ImageIcon(url);

          button.setIcon(icon);
            button.repaint();
            button.validate();
            en = false;
        }
        else {
                String file2 = "file:///c:/jdk1.2/sources/dk/des/lal/test2.jpg";

            URL url = new URL(file2);
            ImageIcon icon = new ImageIcon(url);

            button.setIcon(icon);
            button.repaint();
            button.validate();

          en = true;
        }
        } catch (MalformedURLException mue) {
            mue.printStackTrace();
        } catch (java.io.IOException ioe) {
            ioe.printStackTrace();
        }
    }


When I run this it loads the two images just fine, but if I change one of the images it does not register this in my button...

Is it some sort of cache, and how do I avoid it ?
0
Comment
Question by:marss
  • 5
  • 3
8 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230994

I don't understand quite well your problem.
When you change one of the .jpeg files and what code do you use to set the new image to your button ?

or you have tried this code
 String file1 = "file:///c:/jdk1.2/sources/dk/des/lal/test1.jpg";
 URL url = new URL(file1);
 ImageIcon icon = new ImageIcon(url);
 button.setIcon(icon);
again, and it does not show the new .jpeg version on the button ?

also what is your platform / JDK version / Swing version

regards
  heyhey
0
 

Author Comment

by:marss
ID: 1230995
I'll try to explain...

I'm trying to change the image on a JButton dynamically.
At the start I load some image x from a URL, then at some point I want to change the image on the JButton to a new image.
BUT the path must not change.

I'll change the actual data of the image by replacing the file on the harddisk.

 * copy x to harddisk (path file:///test.jpg)
 * load image into button
 * copy y to harddisk (path file:///test.jpg)
 * load image into button

I hope this makes sense :-)

thanx
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230996
is this application or applet ?
if this is applet, browser may perform some sort of caching which is not accessible for your Java applet.
if you are making application, than this effect should be quite easy to implement.

I can build (and test) a small example if you describe your target platform.


0
 

Author Comment

by:marss
ID: 1230997
It is an application.

JDK1.2 (final)
WinNT4.0

And yes it should be quite easy... :-)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:heyhey_
ID: 1230998
try this code for loading new Icon. I don't think that Java will cache reading from file (although it probably will cache reading from URL)

String file1 = "file:///c:/jdk1.2/sources/dk/des/lal/test1.jpg";
Image img = Toolit.getDefalutToolkit().createImage(file1);
ImageIcon icon = newImageIcon(img);
button.setIcon(icon);

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1230999
of course you need real file name :(

String file1 = c:/jdk1.2/sources/dk/des/lal/test1.jpg";
0
 

Author Comment

by:marss
ID: 1231000
It worked ....

thanx a bunch
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 1231001
so i'm posting an answer ;)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapAB Challlenge 35 89
java set up 1 47
Java passing arguments (type error) 2 35
JList custom Cell Renderer refresh 15 43
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…
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 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:

762 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