我在项目中使用JavaFX,Hibernate,Spring。
我需要用我的对象值填充组合框。 在我的组合框中,我只需要显示模型中的标题值。
我的模特课:
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 +
'}';
}
}
和
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 +
'}';
}
在我的控制器中,有一些用于组合框的方法监听器可以对这些数据进行一些其他操作: (关于我从this question和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);
}
}
};
}
});
}
这是我的正确对象,但是,我仍然无法理解如何格式化我的组合框以仅显示我的扇区和其他对象的标题字段? 你能展示一些有效/正确的例子来格式化我的组合框输出吗?
编辑1: 在我的init方法中,我只是将我的对象列表添加到组合框中。我不确定这是否正确,但如果我想在选择组合框值后验证这些数据 - 我必须在组合框中设置一个完整的对象:
@FXML
public ComboBox sectorComboBox;
@Override
public void initialize(URL location, ResourceBundle resources) {
sectorComboBox.setItems(FXCollections.observableArrayList(sectorService.listAll()));
}
答案 0 :(得分:6)
您应该使用StringConverter
而不是覆盖ListCell。这是实现相同结果的更优雅方式。
StringConverter<Sector> converter = new StringConverter<Sector>() {
@Override
public String toString(Sector object) {
return object.getTitle();
}
@Override
public Sector fromString(String string) {
return null;
}
};
<强> MCVE 强>
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
public class ComboBoxConverter extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
ComboBox<Sector> comboBox = new ComboBox<>();
StringConverter<Sector> converter = new StringConverter<Sector>() {
@Override
public String toString(Sector object) {
return object.getTitle();
}
@Override
public Sector fromString(String string) {
return null;
}
};
comboBox.setConverter(converter);
comboBox.setItems(FXCollections.observableArrayList(new Sector("Title1", 24), new Sector("Title2", 50)));
comboBox.getSelectionModel().selectFirst();
VBox box = new VBox(comboBox);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
}
扇区类是一个带有两个字段的简单POJO。
public class Sector {
private String title;
private double area;
public Sector(String title, double area) {
this.title = title;
this.area = area;
}
public double getArea() {
return area;
}
public void setArea(double area) {
this.area = area;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}