0

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);

        }
Yonex
  • 87
  • 9
  • Check here : https://stackoverflow.com/questions/23176624/javafx-freeze-on-desktop-openfile-desktop-browseuri – JKostikiadis Oct 01 '17 at 14:17
  • I was facing exact same problem. Solved! Thanks a lot. :) – Yonex Oct 01 '17 at 14:34
  • You know that turning the File into a URL and removing "file:" will create an *invalid* file path if there happen to be any [“reserved”](https://tools.ietf.org/html/rfc2396#section-2.2) characters in any part of the file name, right? Why the elaborate round trip? Why not just `filePath = file.toString();`? – VGR Oct 01 '17 at 15:02
  • @VGR And then the string is used to create a new `File` instance... – James_D Oct 01 '17 at 16:27

0 Answers0