I'm trying to populate a ListView with the name property of an Object in JavaFX.
I'm also using an FXML controller.
The ListView
is bound to the fx:id stationListView
.
I have an ObservableList
set up as such:
@FXML
ObservableList<Station> stations = getStations();
Where getStations();
returns a list of Station
objects with various properties assigned, with the name of each station accessible using station.getName()
.
I am able to add the name of each Station
to the ListView
if I modify the ListView:
//Initialising ListView
@FXML
private ListView stationListView;
//In 'initialise' section of FXML controller
for(Station station:stations) {
stationListView.getItems().add(station.getName());
}
However I want to populate the ListView
with the Station
objects so that I can update a TableView
based on the other properties of these objects.
Im still new to Java so any pointers in the right direction are appreciated.
EDIT
I was able to set the names of the stations in the ListView
using a cell factory:
stationListView.setCellFactory(new Callback, ListCell>() {
@Override
public ListCell<Station> call(ListView<Station> param) {
final ListCell<Station> cell = new ListCell<Station>() {
@Override
public void updateItem(Station item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getName());
}
}
};
return cell;
}
});
stationListView.setItems(stations);
However now I don't know how to update the TableView
based on the object selected in the ListView