1

When I click on "Show dialog" button, it appears a JDialog that has a JLabel with an Icon (image), if the file "C:\Test\Test.png" exists the image appears correctly. I have an external process that during the day updates this image (always using the same name "Test.png"). But my problem is that, even I dispose the jdialog and the image is updated (the app is still running), when I click again on "Show dialog" that jdialog is shown with the old image (looks like the old image is in buffer in somewhere). Even if I close the jdialog, delete the file e open it again the image is shown.

Is there a way to prevent it?

What I need is when I open the jdialog it shows the updated image.

import java.awt.Dimension;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;

public class ShowImage implements Runnable {

    @Override
    public void run() {
        createGUI();
    }

    public void createGUI() {
        JButton jButton = new JButton("Show dialog");
        jButton.addActionListener((e) -> new ShowImageWindow());

        JFrame jFrame = new JFrame();
        jFrame.add(jButton);
        jFrame.pack();
        jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jFrame.setLocationRelativeTo(null);
        jFrame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ShowImage());
    }
}

class ShowImageWindow extends JDialog {
 
    private JLabel imagemJLabel;

    public ShowImageWindow() {
        initComponents();
    }
    
    public void initComponents() {
        Icon preview = new ImageIcon("C:\\Test\\Test.png");
        System.out.println(preview.toString());
        imagemJLabel = new JLabel();
        imagemJLabel.setHorizontalAlignment(SwingConstants.CENTER);
        imagemJLabel.setIcon(preview);
        
        JPanel contentPane = new JPanel();
        contentPane.add(this.imagemJLabel);
        
        setSize(new Dimension(500, 500));
        setModal(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setContentPane(contentPane);
        setVisible(true);
    }
}

Thanks.

  • You can put the boolean in the main class, and pass it in as a parameter to the ShowImageWindow constructor if you absolutely want to re-create the dialog each time – Hovercraft Full Of Eels Jan 08 '21 at 12:43
  • Hi @HovercraftFullOfEels I'm not sure if I got it. I created this question because my main problem is: there is an external process that during the day is updating the image (i.e. C:\Test\Test.png) always using the same name "Test.png". But the way my code is always show the first image when I opened the app. Even I'm always re-creating the jdialog is always shown the first image.. even if I delete it or update it.. (sorry for my bad English). Thanks. – Aislan Nadrowski Jan 08 '21 at 12:53
  • Shouldn't you be putting this information in the main question? Without it the question is invalid and my answer and efforts meaningless. Please read through the [ask] and then [edit] your question providing *all* the key details, the above information *and more* – Hovercraft Full Of Eels Jan 08 '21 at 12:55
  • Again, add all the details, including how and when the image changes, what process changes it, .... enough information so that people don't have to guess at anything – Hovercraft Full Of Eels Jan 08 '21 at 12:58
  • @HovercraftFullOfEels you're right. sorry. let me improve it. – Aislan Nadrowski Jan 08 '21 at 13:02
  • 1
    I already answered this here: [Java: ImageIcon - image file updating but image icon in Java frame not](https://stackoverflow.com/questions/60952313/java-imageicon-image-file-updating-but-image-icon-in-java-frame-not) – Mark Jeronimus Jan 08 '21 at 13:29
  • apologies -- caching is involved. Have you tried getting your image using `ImageIO.read(...)`? 1+ to @MarkJeronimus's answer, thank you – Hovercraft Full Of Eels Jan 08 '21 at 13:36
  • I think `Toolkit.getDefaultToolkit().createImage` is usually the safer way to load images for *display*, whereas I use `ImageIO.read` to load images for analysis/manipulation. – Mark Jeronimus Jan 08 '21 at 13:40
  • If @MarkJeronimus's answer in the other question helped you, please be sure to up-vote it – Hovercraft Full Of Eels Jan 08 '21 at 13:45
  • @HovercraftFullOfEels how can I do that? – Aislan Nadrowski Jan 08 '21 at 13:48
  • Done! but.. "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Thank you all! – Aislan Nadrowski Jan 08 '21 at 14:11

0 Answers0