1

My goal is to display images that are selected from a filechooser activated by button, and add those images to my gridpane. I am able to get the right URL file path name from the file chooser in order to make a correct imageview however when doing so my gridpane does not show any images being added..

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();
                String megaUrl;
                try {
                    megaUrl = Paths.get(megaPath).toUri().toURL().toString();
                } catch (MalformedURLException e) {
                    throw new IllegalArgumentException(e);
                }

                parentPath = selectedFile.getParent();
                System.out.println("wholePath: " + wholePath);
                System.out.println("parent: " + parentPath);
                System.out.println("File Name: " + name);
                System.out.println("megaPath: " + megaUrl);
                //System.out.println("Canonical: " + Canonical);

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

I've tried multiple ways of insterting an images and these are the filepaths im getting:

wholePath: \\jupiter\yr1005\Desktop\20190111_1340501.jpg
parent: \\jupiter\yr1005\Desktop
File Name: 20190111_1340501.jpg
megaPath: file://jupiter/yr1005/Desktop/20190111_1340501.jpg

(im using the megapath)

basically when i choose an image from filechooser I get no error but no image is shown after selection. I just get all the print statements in return.. an idea why?

This is my create Display method:

   public void createDisplay(Stage primaryStage) {

   primaryStage.setTitle(this.MONKEY_TITLE);
   GridPane paneofgridmonkeys = new GridPane();
   paneofgridmonkeys.setAlignment(Pos.CENTER);
   paneofgridmonkeys.setVgap(10);
   paneofgridmonkeys.add(browseButton, 10, 10);

   ScrollPane allTehFaces = new ScrollPane(paneofgridmonkeys);
   allTehFaces.setFitToWidth(true);
       primaryStage.setScene(new Scene(allTehFaces, 500, 500));primaryStage.show();
     }
   }
Zieryn
  • 27
  • 6
  • What does the createDisplay method look like? – Bill Horvath May 20 '19 at 15:50
  • I have updated my post to show it. Maybe i should make a createImage method that i will call at the end of my button method to create the view? – Zieryn May 20 '19 at 15:52
  • There's your problem. Try removing this line: `GridPane paneofgridmonkeys = new GridPane();` – Bill Horvath May 20 '19 at 15:55
  • You're creating a new instance of GridPane using the same variable name (`paneofgridmonkeys`) as the class-scoped instance, which I'm inferring is a class-level variable because it's referenced in `makeBrowseButton`. The ability to create locally-scoped variables with the same name as class-scoped variables (and which then supersede the class variables locally) is one of the flaws in the design of Java IMO. – Bill Horvath May 20 '19 at 15:57
  • thank youuu! that makes so much more sense now. – Zieryn May 20 '19 at 16:01
  • You're very welcome! I corrected my answer and undeleted it, below. – Bill Horvath May 20 '19 at 16:03

1 Answers1

1

You're problem is in the createDisplay method; specifically this line:

   GridPane paneofgridmonkeys = new GridPane();

Here you're creating a locally-scoped GridPane called paneofgridmonkeys, which must be the name of another class-level variable called paneofgridmonkeys, since it's available to makeBrowseButton. When you do this in local scope, the new instance you've created becomes the one that's used inside of that method, rather than the class-level instance; thus the class-level one isn't the one being added to your scene, and you're not seeing the changes.

Bill Horvath
  • 1,336
  • 9
  • 24
  • @billHovath do you know what imports i would need for invalidate and repaint because i cant seem to find it in the Java oi – Zieryn May 20 '19 at 15:23
  • I inferred paneofgridmonkeys was an instance of JComponent (or one of it's child classes.) Is that not the case? What class is it? – Bill Horvath May 20 '19 at 15:26
  • no it is you are correct but I am getting 'cannot find symbol' error pointing at invalidate and repaint and i assumed thats missing imports on my part. – Zieryn May 20 '19 at 15:28
  • Nm; looks like you're using JavaFX instead of Swing; my bad. I'll edit my answer. – Bill Horvath May 20 '19 at 15:33
  • Still no image displayed... should i start thinking about adding the url to an array and then makign a for loop in my create display method to add images.. Been having this issue for quite some time now (i am using javafz sorrry for the confusion). – Zieryn May 20 '19 at 15:37
  • Hmm... Maybe adding `paneofgridmonkeys.requestFocus();` before `paneofgridmonkeys.requestLayout();` will help? (Sorry; it's been _forever_ since I did JavaFX) – Bill Horvath May 20 '19 at 15:40
  • Still not displaying and this is all inside the actionHanlder so it should work – Zieryn May 20 '19 at 15:41