0

It's my first time ever using this site as well as javafx so I'll just get straight to it.

I have a tableview created in xml with an id "productTable" located within the file "test.fxml" and I have a button that is meant to populate the tableview so I have a controller that is linked to the button. So when my button is pressed it sends my event handler a ActionEvent object. The problem is that since I have the tableview created in FXML I do not have a reference to it in code. I'd like to be able to do something like:

    private void handleButtonAction(ActionEvent event) {
    String buttonId = ((Button)event.getSource()).getId();

    //What I want to do/get
    TableView table = FXMLLoader.load(getClass().getResourceById("test.fxml", "productTable"))
}

To get a reference to my table via a variable in the code.

  • 1
    Read _[Introduction to FXML](https://docs.oracle.com/javase/10/docs/api/javafx/fxml/doc-files/introduction_to_fxml.html)_. You're going to want to put `@FXML private TextField productTable` in your controller class. – Slaw Jul 14 '18 at 04:19

1 Answers1

0

In your controller class, simply add a reference to the TableView using the @FXML annotation:

@FXML
private TableView productTable;

The @FXML tells JavaFX that this object is created within the FXML file, not the Java code itself.

From there, you may reference the TableView just like any other object.

Zephyr
  • 9,885
  • 4
  • 28
  • 63