Gordon Freeman Turtle
Gordon Freeman Turtle

Reputation: 93

How to get a property from an observablelist with multiple properties

private final ObservableList<City> cityList = FXCollections.observableArrayList(city
        -> new Observable[]{
            city.cityNameProperty(),
            city.cityLonProperty(),
            city.cityLatProperty()});

public ObservableList<City> getCityList() {
    return cityList;
}

How can I retrieve the cityNameProperty from one object inside the observableArrayList for setting a listview?

I did

cityView.setItems(cityList.getCityList());

But this is setting the objects ([email protected]), whereas I need the properties of the objects.

Upvotes: 0

Views: 1179

Answers (1)

James_D
James_D

Reputation: 209684

Use a cell factory:

cityView.setCellFactory(lv -> new ListCell<City>() {
    @Override
    protected void updateItem(City city, boolean empty) {
        super.updateItem(city, empty);
        setText(empty ? null : city.getCityName());
    }
});

Upvotes: 1

Related Questions