0

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

Joshua Dunn
  • 71
  • 1
  • 6
  • `stationListView.setItems(stations);`??? Possibly using a custom cell factory, if `Station.toString` does not return the appropriate results... – fabian Apr 10 '18 at 10:38
  • @fabian That added all the objects to the ListView but doesn't set their name as what is displayed – Joshua Dunn Apr 10 '18 at 10:42
  • @fabian please don't even mention toString() ;) As _you_ well know that it must not be implemented for application needs, but _others_ tend to pick up dirty habits ;) – kleopatra Apr 10 '18 at 10:44
  • @kleopatra Do you have a link to a 'decent tutorial' ? I haven't been able to find one thus far. – Joshua Dunn Apr 10 '18 at 10:50
  • ohh ... just noticed that the info is no as helpful as I thought ;) And Oracle's tutorial uses String as items ... dooohhh. The info entry "How to example" at least has code ... – kleopatra Apr 10 '18 at 11:07
  • okay, I was wrong with my advice, sry: if somebody doesn't know where to look exactly there's nothing obvious. – kleopatra Apr 10 '18 at 11:17
  • from the api doc of ListView _to track selection and focus, it is necessary to become familiar with the SelectionModel and FocusModel classes_ ;) And I just added a reference to Oracle's ui tutorial ... – kleopatra Apr 10 '18 at 11:52
  • @kleopatra But it will be possible to update a TableView with an objects properties based on the selection of the object in the ListView? Just want to know that I'm not barking up the wrong tree. – Joshua Dunn Apr 10 '18 at 11:59
  • yes, it certainly is :) – kleopatra Apr 10 '18 at 12:00
  • Also see the [CodeMakery event handling tutorial](http://code.makery.ch/blog/javafx-8-event-handling-examples/). The section on **"ListView events"** contains an example with a list view with a user-defined class for the item type. – James_D Apr 10 '18 at 12:50
  • Please don't ask another question within your question. Please open a new question if you're going to ask another one. – AJF Apr 10 '18 at 17:45

0 Answers0