0
Problem:
Code to demonstrate the problem:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class viewImageFX extends Application {
private Group g;
@Override
public void start(Stage primaryStage) {
BorderPane pane = new BorderPane();
Button btnOpen = new Button("Open");
Button btnDelete = new Button("Delete");
btnOpen.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
g = createImagewithDetails();
pane.setCenter(g);
}
});
btnDelete.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
g = null;
Label lbl = new Label("eliminado");
pane.setCenter(lbl);
}
});
HBox menu = new HBox();
menu.getChildren().addAll(btnOpen, btnDelete);
pane.setBottom(menu);
Scene scene = new Scene(pane, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private Group createImagewithDetails() {
ImageView img = new ImageView();
Group group = new Group();
final Image image;
try {
image = new Image(new FileInputStream(new File("D:\\movie_play_red.png")), 150, 0, true, true);
img.setImage(image);
} catch (FileNotFoundException ex) {
System.out.println("Problema no load da Imagem ");
Logger.getLogger(viewImageFX.class.getName()).log(Level.SEVERE, null, ex);
}
Label lblDescricao = new Label("asflsanfa asisoafgsang asiga");
group.getChildren().addAll(img, lblDescricao);
return group;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
How can I force the file to stop being used?
It’s not supposed to, if you put the Group
returned to null
that the reference to the file is lost and that forms the release?