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
Solved

How to use ImageIcon

Posted on 1999-01-14
8
564 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented programming comparison 5 77
expectj telnet failing 5 45
pagenation logic how it is working in my code 1 53
Bot application - advice 3 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

840 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