0

Dears, I'm working in a JAVAFX application and i have a component called "Disjuntor" which the user can add as many as it wants. So I have a scene where I can add those elements to a GripPane. I created a "fxml template" for the Disjuntor, and intended to call it every time time the user tries to add a new one, filling ti with proper data.

My main scene with the GridPane highlighted

enter image description here

My disjuntor fxml template

enter image description here

    Parent disjuntorPane = null;
    Image image = null;
    try {
        disjuntorPane = FXMLLoader.load(DisjuntorController.class.getResource("disjuntorView.fxml"));
        image = new Image (this.getClass().getClassLoader().getResourceAsStream("resource/img/disjuntor.png"));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ImageView imageViewComponente = (ImageView) disjuntorPane.lookup("#imageViewComponente");
    imageViewComponente.setImage(image);
    this.componentesGridPane.add(disjuntorPane, 0, 0);

Once I got the template I'd like to update it with an image.

So trying to use that template seemed very hard, and I'm wondering if I'm going down the wrong path. Is this the way? Are there other possible and better ways to add several panes inside my scene with different data?

Thanks for the help.

coder
  • 8,346
  • 16
  • 39
  • 53
  • 1
    Use [this pattern](https://docs.oracle.com/javase/9/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html#custom_components) – James_D May 07 '18 at 16:08
  • If you don't use the approach suggested by James, take a look here to learn how to properly pass data to a scene without relying on looking up nodes based on CSS selectors: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml Also I recommend rewriting the code a bit. If `FXMLLoader.load` throws an exception you get a NPE just below the `catch`... – fabian May 07 '18 at 16:20

0 Answers0