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.
– user28595
I provided the codes, the methods used to do what was mentioned at the beginning is in the codes, this is the example!
– Hamon
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
– user28595
Sorry for the mistake. I hope you’re correct now.
– Hamon
It is not yet a complete and executable minimum example. Please read the links to see how to do this.
– user28595
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.
– Hamon
I took everything else, left only the methods.
– Hamon