This discussion is archived
6 Replies Latest reply: Nov 9, 2012 7:58 PM by 966041 RSS

FileChooser can't open the file

966041 Newbie
Currently Being Moderated
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
    EricLePonner Explorer
    Currently Being Moderated
    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 Guru
    Currently Being Moderated
    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 Expert
    Currently Being Moderated
    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 Guru
    Currently Being Moderated
    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 Expert
    Currently Being Moderated
    Hi James. Yes, you are right. I didn't pay attention to that small detail.
  • 6. Re: FileChooser can't open the file
    966041 Newbie
    Currently Being Moderated
    Thank you all for your kind suggestion.
    It worked for me.I will try to improve the way of posting my questions.

    Thanks

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points