Solved

How to use ImageIcon

Posted on 1999-01-14
8
575 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:marss
ID: 1230997
It is an application.

JDK1.2 (final)
WinNT4.0

And yes it should be quite easy... :-)
0
 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…

734 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