6 Replies Latest reply: Nov 9, 2012 9:58 PM by 966041 RSS

    FileChooser can't open the file

    966041
      I use the FileChooser to open the file(image) and bring to an anchor pane.The open dialog window appears and i can select the file but it fails to bring it to pane.
      Can any one help me.


      public void openImageFired() {

      try{

      FileChooser fileChooser = new FileChooser();
      FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image Files","*.png","*.jpg");
      fileChooser.getExtensionFilters().add(extFilter);

      File file = fileChooser.showOpenDialog(null);

      if(file!=null)//show the image in the rightPane.
      path=file.getCanonicalPath();

      path=path.replace('\\', '/');
      System.out.println(path);

      image.setImage(new Image(path));
      rightPane.getChildren().add(image);///rightPane is the anchor pane

      }catch(Exception e){
      e.printStackTrace();
      }

      System.out.println("Hello");
      }
        • 1. Re: FileChooser can't open the file
          Eric Le Ponner
          Assuming that:

          image.setImage(new Image(path));

          is correctly executed, I would check rightPane layout configuration ;
          may be it is zero-sized ; the image layoutX/Y also need to be checked.

          You can also replace the AnchorPane by a StackPane and see what
          happens.

          My 2 cents.
          • 2. Re: FileChooser can't open the file
            James_D
            First, can you please format your code? Some of it isn't readable (for example, the path.replace(...) call, and your if statement looks wrong). Go back and edit your question and format the code as in the "How to post code" thread at the top of the forum. That will also prevent the asterisks in the ExtensionFilter constructor call from being interpreted as markup for bold text.

            Are you seeing an Exception?

            The path is probably wrong. File.getCanonicalPath() returns a string representing the absolute path of the file (i.e. relative to the filesystem root). The Image constructor taking a String expects a path relative to the classpath. You can either do
            image.setImage(new Image("file:"+path));
            which creates a file URL from the path, or (probably better) load from an input stream:
            image.setImage(new Image(new FileInputStream(file)));
            The second option means you don't have to mess with the path at all.
            • 3. Re: FileChooser can't open the file
              shakir.gusaroff
              Wrong extension filter. * is missing.
              The following works for me:
                 public void openImageFired() {
                  
                      try {
              
              
                          FileChooser fileChooser = new FileChooser();
                          FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg");
                          fileChooser.getExtensionFilters().add(extFilter);
              
                          File file = fileChooser.showOpenDialog(null);
                          ImageView image = new ImageView(); 
                          image.setImage(new Image(new FileInputStream(file)));
                          rightPane.getChildren().add(image);///rightPane is the anchor pane
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
              
              
                      System.out.println("Hello");
                  }
              • 4. Re: FileChooser can't open the file
                James_D
                Shakir: He had the asterisks in the extension filter; they just got removed as they were interpreted as markup for bold text. (If you hit reply and quote the original, you'll see them.)

                The problem was with the path.

                I pointed both of these out in my previous post.
                • 5. Re: FileChooser can't open the file
                  shakir.gusaroff
                  Hi James. Yes, you are right. I didn't pay attention to that small detail.
                  • 6. Re: FileChooser can't open the file
                    966041
                    Thank you all for your kind suggestion.
                    It worked for me.I will try to improve the way of posting my questions.

                    Thanks