JavaFx ImageView does not change the image


This is in JavaFX:

I have a product dashboard that (in a sidebar) shows the photo of the currently selected product in a TableView. The data of that selected product can be edited in an additional window. The edition allows you to change the product's photography. This change is made by overwriting the original photo (that is, it changes the content of the file, not its name or its location on disk).

My problem is that when that photo is changed, when leaving the editing window, the dashboard does not reflect that change, it still shows the previous photo (which makes some sense because the name of the file remains the same), so first "clean" the photo that is already shown doing

fotoProducto.setImage(new Image(null)); // Esto si funciona

And then I upload the file again:

fotoProducto.setImage(new Image("archivo.jpg"));

However, the dashboard still shows the previous photo and not the new one.

As I said, what changes is the content of the image, not its name on disk (for example, it will always be called "image1.jpg"). Is there some type of cache that was cleaned or some way to force the ImageView to refresh and recognize the change in the contents of the file?

Thanks for any ideas.

asked by Juan Carlos 29.11.2018 в 15:35

2 answers


After two weeks I finally found an answer:

Instead of loading the images using fotoProducto.setImage(new Image("archivo.jpg")); , now I do it like this:

File img = new File("archivo.jpg"); InputStream isImage = (InputStream) new FileInputStream(img); imageView.setImage(new Image(isImage));

That solved the problem.

answered by 09.12.2018 / 22:35

Use fotoProduct.repaint ();

answered by 29.11.2018 в 19:32