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.