Solved

How to use ImageIcon

Posted on 1999-01-14
8
552 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
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 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

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
Understanding websocket example in spring 1 57
Which is the latest version of Apache Tomcatl 3 50
Free Alternative to JIRA 4 91
javap bin 2 30
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

861 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

24 Experts available now in Live!

Get 1:1 Help Now