I am trying to choose an image file by filechooser and open it with the default associated program(linux)
In my javafx UI I have 2 button select image & view image. By select image button I am taking image path(needed for further calculation) and by view image i m trying to open the selected image with the default associated program in linux.
The problem is whenever i run this program select image button works properly.(It is giving right path. I have tested it by printing) But when I click view image my UI become unresponsive and any image file is not opening by the default associated program.
I cant catch my fault? Can anyone help please?
Stage = window;
String currentPath;
String filePath;
Button selectimage = new Button("Select");
Button viewimage = new Button("Result");
selectimage.setOnAction(e->{
File file= fileChooser.showOpenDialog(window);
if (file != null)
{
try {
currentPath = file.toURI().toURL().toExternalForm();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
};
//System.out.println(currentPath);
filePath=currentPath.replace("file:", "");
//System.out.println(filePath);
}
});
viewimage.setOnAction(e->{
File imageFile = new File(filePath);
try {
open(imageFile);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
});
As in currentPath I have extra "file:", I removed it to get absolute path of the selected image. and my open() function look like this.
public static void open(File document) throws IOException {
Desktop dt = Desktop.getDesktop();
dt.open(document);
}