无法从String转换为ObservableValue< String> [英] cannot convert from String to ObservableValue<String>

查看:1213
本文介绍了无法从String转换为ObservableValue< String>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序来管理和显示有关机场,航班等的数据。
事实是我有一个带有几个tableColumns的tableView(在javafx中),我想在每一列上显示一些信息(命运,起源,公司等),所以我键入了这个:

I'm making a program to manage and show data about airports, their flights and so on. The fact is that I have a tableView (in javafx) with several tableColumns, and I want to show some information (destiny, origin, company, etc) on each column so I typed this:

@FXML
private TableColumn<Flight, String> destinoCol;

@FXML
private TableColumn<Flight, String> numCol;

@FXML
private MenuButton aeropuerto;

@FXML
private MenuButton tipo;

@FXML
private Button filtrar;

@FXML
private TableColumn<Flight, LocalTime> horaCol;

@FXML
private Button este;

@FXML
private DatePicker fecha;

@FXML
private TableColumn<Flight, String> origenCol;

@FXML
private Label retrasoLabel;

@FXML
private ImageView companiaImg;

@FXML
private VBox detalles;

@FXML
private Button todos;

@FXML
private ImageView avionImg;

@FXML
private Label tipoLabel;

private mainVuelos m;
private List<Airport> aeropuertos;
private Data data;
@FXML
void initialize() {
    data = Data.getInstance();
    aeropuertos = data.getAirportList();
    List<MenuItem> ItemAeropuertos = new LinkedList<MenuItem>();
    for (int i = 0; i < aeropuertos.size(); i++) {
        MenuItem item = new MenuItem(aeropuertos.get(i).getName());
        item.setOnAction((event) -> cambiarAer(event));
        ItemAeropuertos.add(item);
    }
    aeropuerto.getItems().setAll(ItemAeropuertos);
    destinoCol.setCellValueFactory(cellData -> cellData.getValue().getDestiny());
}

方法getDestiny(),因为它表示返回特定航班的命运因为很明显我不能使用最后一条指令,它说无法从String转换为ObservableValue,但我真的不知道如何解决它以便能够显示该列的命运。谢谢大家。

The method getDestiny(), as it says returns de destiny of a specific flight as a String so obviously I cannot use the last instruction, it says "cannot convert from String to ObservableValue" but I don't really know how to solve it in order to be able to show the destinies on that column. Thank u all.

推荐答案

根据 Javadocs setCellValueFactory(...)期望 Callback< CellDataFeatures< Flight,String>,ObservableValue< String>> ,即采用 CellDataFeatures< Flight,String> 作为参数,并产生 ObservableValue< String>

According to the Javadocs, setCellValueFactory(...) expects a Callback<CellDataFeatures<Flight, String>, ObservableValue<String>>, i.e a function that takes a CellDataFeatures<Flight, String> as its parameter, and results in an ObservableValue<String>.

正如错误消息所示,您的函数求值为字符串 cellData.getValue ()。getDestiny()),这是不正确的类型。

As the error message says, your function evaluates to a String (cellData.getValue().getDestiny()), which is not the correct type.

根据您的实际要求,您有两种选择。

You have two choices, depending on your actual requirements.

您可以动态创建类型正确的东西:最简单的方法是使用 ReadOnlyStringWrapper

Either you can create something on the fly that is of the correct type: the easiest thing to use is a ReadOnlyStringWrapper:

destinoCol.setCellValueFactory(cellData -> new ReadOnlyStringWrapper(cellData.getValue().getDestiny()));

这将显示正确的值,但不会很好地连线到该属性飞行物体。如果您的表是可编辑的,编辑将不会自动传播回基础对象,并且从其他地方对基础对象的更改将不会自动更新到表中。

This will display the correct value, but won't be nicely "wired" to the property of the flight object. If your table is editable, edits won't automatically propagate back to the underlying object, and changes to the underlying object from elsewhere won't automatically update in the table.

如果你需要这个功能(这可能是一个更好的方法),你应该实现你的模型类 Flight 来使用 JavaFX属性

If you need this functionality (and this is probably a better approach anyway), you should implement your model class Flight to use JavaFX properties:

public class Flight {

    private final StringProperty destiny = new SimpleStringProperty();

    public StringProperty destinyProperty() {
        return destiny ;
    }

    public final String getDestiny() {
        return destinyProperty().get();
    }

    public final void setDestiny(String destiny) {
        destinyProperty().set(destiny);
    }

    // similarly for other properties...
}

然后你可以做

destinoCol.setCellValueFactory(cellData -> cellData.getValue().destinyProperty());

这篇关于无法从String转换为ObservableValue&lt; String&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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