JavaFX组合框没有从对象显示正确的值 [英] JavaFX combobox didn't show correct value from objects

查看:1168
本文介绍了JavaFX组合框没有从对象显示正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用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(){
returnSector {+
id ='+ getId() \''+
title ='+ title +'\''+
,stageList =+ stageList +
'}'
}
}

  public class Stage extends标识符{
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(){
returnStage {+
id ='+ getId() \''+
name ='+ name +'\''+
,firstStation =+ firstStation +
,secondStation =+ secondStation +
,communicationDistanceList =+ communicationDistanceList +
'}';
}

在我的控制器中有一些方法监听器为combobox做一些其他的操作与此数据:
(关于单元格工厂我从



这是我正确的对象,但是我还是不明白如何格式化我的组合框,字段从我的部门和其他对象?
你能显示一些有效/正确的例子来格式化我的组合框输出吗?



编辑1:
在我的init方法中,我只是将我的对象列表添加到combobox。我不知道这是正确的方式,但如果我想验证这个数据后combobox值被选择 - 我必须在组合框中设置一个完整的对象:

  @FXML 
public ComboBox sectorComboBox;

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


解决方案

c $ c> 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应用程序{
@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 = new Scene(box,200,200);
primaryStage.setScene(scene);
primaryStage.show();
}
}

扇区类是一个具有两个字段的简单POJO。

  public class Sector {
private String title;
私人双面积;

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;
}
}


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()));
        }

解决方案

You should use a StringConverter instead of overriding the ListCell. This is a more elegant way of achieving the same result.

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();
    }
}

The sector class is a simple POJO with two fields.

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;
    }
}

这篇关于JavaFX组合框没有从对象显示正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆