簡體   English   中英

JavaFX組合框未顯示來自對象的正確值

[英]JavaFX combobox didn't show correct value from objects

我在項目中使用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 +
                '}';
    }

在我的控制器中,有一個供組合框使用的方法偵聽器,以對該數據進行其他一些操作:(關於單元工廠,我從這個問題以及從這里都讀取到了

   @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:在我的初始化方法中,我只是將我的對象列表添加到組合框。 我不確定這是正確的方法,但是如果我想在選擇組合框值之后驗證此數據-我必須在組合框中設置一個完整的對象:

  @FXML
        public ComboBox sectorComboBox;

    @Override
        public void initialize(URL location, ResourceBundle resources) {
            sectorComboBox.setItems(FXCollections.observableArrayList(sectorService.listAll()));
        }

您應該使用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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM