I am using JavaFX, Hibernate, Spring in my project.
And i need to fill the combobox with my object values. In my combobox i need to show only title value from my model.
My model class:
public class Sector extends Identifier {
private String title;
private List<Stage> stageList;
public List<Stage> getStageList() {
return stageList;
}
public void setStageList(List<Stage> stageList) {
this.stageList = stageList;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "Sector{" +
"id='" + getId() + '\'' +
"title='" + title + '\'' +
", stageList=" + stageList +
'}';
}
}
and
public class Stage extends Identifier {
private String name;
private Station firstStation;
private Station secondStation;
private List<CommunicationDistance> communicationDistanceList;
public Stage() {
}
public Stage(String name, Station firstStation, Station secondStation) {
this.name = name;
this.firstStation = firstStation;
this.secondStation = secondStation;
}
public List<CommunicationDistance> getCommunicationDistanceList() {
return communicationDistanceList;
}
public void setCommunicationDistanceList(List<CommunicationDistance> communicationDistanceList) {
this.communicationDistanceList = communicationDistanceList;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Station getFirstStation() {
return firstStation;
}
public void setFirstStation(Station firstStation) {
this.firstStation = firstStation;
}
public Station getSecondStation() {
return secondStation;
}
public void setSecondStation(Station secondStation) {
this.secondStation = secondStation;
}
@Override
public String toString() {
return "Stage{" +
"id='" + getId() + '\'' +
"name='" + name + '\'' +
", firstStation=" + firstStation +
", secondStation=" + secondStation +
", communicationDistanceList=" + communicationDistanceList +
'}';
}
In my controller there is some method-listener for combobox to make some other manipulations with this data: (about cell factory i read from this question and also from here)
@FXML
public void currentSectorSelected(ActionEvent actionEvent) {
ObservableList<Stage> observableList = FXCollections.observableArrayList(((Sector) sector.getSelectionModel().getSelectedItem()).getStageList());
stage.setItems(observableList);
stage.getSelectionModel().selectFirst();
stage.setCellFactory(new Callback<ListView<Stage>, ListCell<Stage>>() {
@Override
public ListCell<Stage> call(ListView<Stage> param) {
return new ListCell<Stage>(){
@Override
public void updateItem(Stage item, boolean empty){
super.updateItem(item, empty);
if(!empty) {
setText(item.getName());
setGraphic(null);
} else {
setText(null);
}
}
};
}
});
}
All is working, but in my comboboxes values shown like these:
This are my correct objects but, still i can't understand how to format my combobox to show only title field from my Sector and other objects? Can you show some valid/correct example to format my combobox output?
Edit 1: In my init method i am simply added the list of my objects to combobox. I am not sure that this is correct way, but if i want to validate this data after combobox value is chosen - i must to set a full object in combobox:
@FXML
public ComboBox sectorComboBox;
@Override
public void initialize(URL location, ResourceBundle resources) {
sectorComboBox.setItems(FXCollections.observableArrayList(sectorService.listAll()));
}