You cannot do this only in an FXML file.
Define the appropriate listView (assuming fx:id="myListView"in FXML) in the Controller class of the FXML file:
@FXML
private ListView<MyDataModel> myListView;
Add a listener in the init / start method, which will listen for changes to the list item:
myListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<MyDataModel>() {
@Override
public void changed(ObservableValue<? extends MyDataModel> observable, MyDataModel oldValue, MyDataModel newValue) {
System.out.println("Selected item: " + newValue);
}
});
MyDataModel String.
,
@FXML
private ListView<String> myListView;
...
...
ObservableList<String> data = FXCollections.observableArrayList("chocolate", "blue");
myListView.setItems(data);
myListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println("Selected item: " + newValue);
}
});