Image does not update

Asked

Viewed 65 times

0

I’m making an application where I use a Jfilechooser to choose an image, I take this image and copy it to a network folder. After that it executes a method that shows this saved image in a Label, transformed into icon.

The problem lives here: When I try to load a different image and save with the same name when displaying this image, it continues as if it were the old one. Only shows the new one when I close the application and open again.

Remembering that I want to keep this IMG name as "1.png"

public void testesComChooser() {

            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("Somente Imagens", "jpg", "jpeg", "gif", "png");

            chooser.setDialogTitle("Selecione uma foto");
            chooser.setFileFilter(filter);
            chooser.setAcceptAllFileFilterUsed(false);

            int resultado = chooser.showSaveDialog(this);

            if(resultado == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                salvarArquivo(file);
            }else {

            }

        }

        private void salvarArquivo(File img){

            String nomeImg = "1.png";
            String caminho = "C:\\imagens\\";

            Path pathDiretorio = FileSystems.getDefault().getPath(caminho, nomeImg);

            try {
             //   Files.deleteIfExists(pathDiretorio);
                Files.copy(img.toPath(), pathDiretorio, StandardCopyOption.REPLACE_EXISTING);

            }catch(Exception ex) {
                ex.printStackTrace();
            }

            visualizarImg(nomeImg);

        }

        private void visualizarImg(String nomeImg) {

            String caminho = "C:\\imagens\\";
            Icon icon = new ImageIcon(Toolkit.getDefaultToolkit().getImage(caminho + nomeImg));
            label.setIcon(icon);
        }
  • Please provide a [mcve] of your code, so it is possible to run and test the problem.

  • I provided the codes, the methods used to do what was mentioned at the beginning is in the codes, this is the example!

  • It is not a complete and verifiable example, as it comes to swing, you need to provide a complete example, these loose methods are not executable. I recommend you read it here: https://pt.meta.stackoverflow.com/a/6252/28595

  • Sorry for the mistake. I hope you’re correct now.

  • It is not yet a complete and executable minimum example. Please read the links to see how to do this.

  • Well, I honestly don’t understand what’s missing, I know I’m a beginner here, but the code is complete. You really need all the code, there’s nothing else to take. I left the code generated by Netbeans only to make it easy to copy.

  • I took everything else, left only the methods.

Show 2 more comments

1 answer

0


Is using the method Toolkit.getImage(String) that, by documentation, not required to return the current content of the file but can return a previously loaded image of that file (uses a cache). A documentation also suggests what to do: call the method flush of the image returned in the previous call:

If the image data contained in the specified file changes, the Image Object returned from this method may still contain Stale information which was Loaded from the file after a prior call. Previously Loaded image data can be Manually discarded by Calling the flush method on the returned Image.

I advise to use the class javax.imagio.ImageIO para ler imagens - method read(File) - is more modern and there’s this problem with Toolkit.

Obs: the Toolkit should not be used directly:

Most Applications should not call any of the methods in this class directly.

  • Thank you very much, buddy. That’s exactly what I needed. I didn’t know about this cache, but I figured it would be something like.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.