Solved

How to use ImageIcon

Posted on 1999-01-14
8
557 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 48
session migration servlets 2 36
eclipse buid path vs tomcat lib path 10 22
Java: anonymous class 4 17
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 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