1

I'm trying to populate a VBox using custom FXML layout.

Anyone know how to get access to controller so I can pass data?

public class Employee implements Initializable {

    @FXML
    private VBox pnItems = null;


    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Node[] nodes = new Node[10];
        for (int i = 0; i < nodes.length; i++) {
            try {

                final int j = i;
                nodes[i] = FXMLLoader.load(getClass().getResource("Item.fxml"));

                //give the items some effect

                nodes[i].setOnMouseEntered(event -> {
                    nodes[j].setStyle("-fx-background-color : #0A0E3F");
                });
                nodes[i].setOnMouseExited(event -> {
                    nodes[j].setStyle("-fx-background-color : #02030A");
                });




                pnItems.getChildren().add(nodes[i]);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Slaw
  • 37,820
  • 8
  • 53
  • 80
Garry
  • 2,256
  • 2
  • 11
  • 14
  • What is wrong with your code? Where is it broken? – nicomp Jan 07 '19 at 14:53
  • i updated the question – Garry Jan 07 '19 at 15:31
  • @kas Take a look at the question/answers linked in my previous comment. You can get the controller from the `FXMLLoader` instance. If you need to get it later based on a `Node` you can store the relation in a `Map`. – Slaw Jan 07 '19 at 15:36
  • i'm a new to java can you help me? – Garry Jan 07 '19 at 16:16
  • if you don't understand the other question, there's no way around going a step back and reading up on java :) – kleopatra Jan 07 '19 at 16:18
  • 1
    Unrelated to the issue: you're only using the array to refer to the nodes from the event handlers and you only refer to the node created in the same iteration. For this reason it's not necessary to use an array. Simply store the result of loading the fxml in a final variable and use that variable instead of refering to a element of the array: `for (int i = 0; i < 10; i++) { final Node node = FXMLLoader.load(getClass().getResource("Item.fxml")); node.setOnMouseEntered(evt -> node.setStyle(...)); ... }` – fabian Jan 07 '19 at 16:55

0 Answers0