Problem:
Codetodemonstratetheproblem:
importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.util.logging.Level;importjava.util.logging.Logger;importjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.control.Label;importjavafx.scene.image.Image;importjavafx.scene.image.ImageView;importjavafx.scene.layout.BorderPane;importjavafx.scene.layout.HBox;importjavafx.stage.Stage;publicclassviewImageFXextendsApplication{privateGroupg;@Overridepublicvoidstart(StageprimaryStage){BorderPanepane=newBorderPane();ButtonbtnOpen=newButton("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?
Is it not supposed, if you put the Group
returned to null
that the reference to the file is lost and that way you release it?