I have a Station
object that extends
javafx.scene.shape.Ellipse
:
public class Station extends Ellipse
And I am trying to put them into a javafx.scene.control.ComboBox
and have that ComboBox
use the toString()
method of my Station
objects instead of using them as a graphic.
ComboBox<Station> metroStationComboBox = new ComboBox<>();
//ADDS A HASHSET OF THE STATIONS
metroStationComboBox.getItems().addAll(dataManager.getStations());
I made a cell factory which will use the toString()
method when they are not selected, but as soon as you select one, it will take the object in as a graphic.:
metroStationComboBox.setCellFactory((ListView<Station> p) -> {
return new ListCell<Station>(){
@Override protected void updateItem(Station item, boolean empty) {
super.updateItem(item, empty);
if (!empty && item != null) {
setText(item.toString());
setGraphic(null);
} else {
setText(null);
}
}
};
});
Is there a way to redefine the selection method so that I can have it use the toString
method of Station
?
(The above code has been reduced for clarity)