0

I am trying to add an image to a gridview using a button that opens up a filechooser that only accepts images. I am getting an exception error when im using the file path from the filechooser to create a setImage to my grid view. I think this is because the path im getting is just not right.

Here is the code where it fails:

public void makeBrowseButton(Stage primaryStage) {

  //attach handler
  browseButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
      FileChooser fileChooser = new FileChooser();                                                                            // create object
      fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg", "*.gif"));    //filter for music files
      //FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
  if ( !parentPath.equalsIgnoreCase("")) {                                                                              //go to previous directory if exists
      File parentPathFile = new File(parentPath);
      fileChooser.setInitialDirectory(parentPathFile);
  }
      File selectedFile = fileChooser.showOpenDialog(primaryStage);

        if (selectedFile != null) {                                                  // display the dialog box
          String wholePath = selectedFile.getPath();
          String name = selectedFile.getName();
          String megaPath = selectedFile.getAbsolutePath();
          parentPath = selectedFile.getParent();
          System.out.println("wholePath: " + wholePath);
          System.out.println("File Name: " + name);
          System.out.println("megaPath: " + megaPath);

         Image newAwesomeImage = new Image(megaPath);
         ImageView view = new ImageView();
         view.setImage(newAwesomeImage);
         paneofgridmonkeys.add(view, 0, 0);
         //paneofgridmonkeys.add(new Label("Changed the image!"), 0, 1);
         createDisplay(primaryStage);
       }}});
     }

The error message is the title it is saying that the exact problem is line:

view.setImage(newAwesomeImage);

as for my system.out results this is what im getting:

wholePath: M:\Home\BenStillerDuckFace.jpg
File Name: BenStillerDuckFace.jpg
megaPath: M:\Home\BenStillerDuckFace.jpg

ive tried all of these and non work. Any ideas?

user3837868
  • 917
  • 1
  • 12
  • 24
Zieryn
  • 27
  • 6

1 Answers1

2

The Image(String url) constructor requires a URL string, not a file name. A file name is not a URL.

To convert a file name string to a URL string, do one of these:

// Java 7+
String megaUrl = Paths.get(megaPath).toUri().toURL().toString();

// Java 1.4+
String megaUrl = new File(megaPath).toURI().toURL().toString();
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • thanks @Andreas that was the issue I am however getting an error now: unreported exception MalformedURLException; must be caught or declared to be thrown pointing at toURL() i tried using a try and catch and got nothing – Zieryn May 16 '19 at 17:32
  • @Zieryn `try { ... } catch (MalformedURLException e) { throw new IllegalArgumentException(e); }` – Andreas May 16 '19 at 17:35
  • Thank you so much! – Zieryn May 16 '19 at 17:41