为什么我的表中显示了StringProperty [value:""] [英] Why StringProperty[value: ""] is displayed in my table

查看:140
本文介绍了为什么我的表中显示了StringProperty [value:""]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 TableView 中显示一些信息,但我面临一些我无法解决的问题。

I'm trying to show some information in a TableView, but I'm facing some issues that I can't fix.

以下是我要做的事情:

我有房间对象:

public class Room {

    private SimpleStringProperty id;
    private SimpleStringProperty description;
    private SimpleStringProperty isForRepair;
    private SimpleStringProperty comment;

    public Room(String id, String description, String isForRepair, String comment) {
        this.id = new SimpleStringProperty(id);
        this.description = new SimpleStringProperty(description);
        this.isForRepair = new SimpleStringProperty(isForRepair);
        this.comment = new SimpleStringProperty(comment);
    }

    public SimpleStringProperty getId() {
        return id;
    }

    public void setId(SimpleStringProperty id) {
        this.id = id;
    }

    public SimpleStringProperty getDescription() {
        return description;
    }

    public void setDescription(SimpleStringProperty description) {
        this.description = description;
    }

    public SimpleStringProperty getIsForRepair() {
        return isForRepair;
    }

    public void setIsForRepair(SimpleStringProperty isForRepair) {
        this.isForRepair = isForRepair;
    }

    public SimpleStringProperty getComment() {
        return comment;
    }

    public void setComment(SimpleStringProperty comment) {
        this.comment = comment;
    }

}

我正在尝试添加我的桌子有些行。这是我的整个代码:

And I'm trying to add some rows to my table. Here is my whole code:

public class RoomsManager {

    private TableView<Room> table = new TableView<>();

    public RoomsManager() {
    }

    public void show() {
        Stage roomsStage = new Stage();

        final ObservableList<Room> data = FXCollections.observableArrayList(
                new Room("1", "The small one", "no", "Good condition")
        );

        Scene scene = new Scene(new Group());
        roomsStage.setTitle("Table View Sample");
        roomsStage.setWidth(355);
        roomsStage.setHeight(500);

        final Label label = new Label("Rooms");
        label.setFont(new Font("Arial", 20));

        table.setEditable(true);

        TableColumn idCol = new TableColumn("ID");
        idCol.setMinWidth(100);
        idCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("id")
        );
        TableColumn descriptionCol = new TableColumn("Description");
        descriptionCol.setMinWidth(100);
        descriptionCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("description")
        );
        TableColumn isForRepairCol = new TableColumn("Is for repair");
        isForRepairCol.setMinWidth(100);
        isForRepairCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("isForRepair")
        );
        TableColumn commentCol = new TableColumn("Comment");
        commentCol.setMinWidth(100);
        commentCol.setCellValueFactory(
                new PropertyValueFactory<Room, String>("comment")
        );
        table.setItems(data);
        table.getColumns().addAll(idCol, descriptionCol, isForRepairCol, commentCol);

        final VBox vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        roomsStage.setScene(scene);

        roomsStage.show();

    }

}

但是我的桌子看起来像这样:

But my table looks like this:

我缺少什么这里?我知道它很小,但作为一个新手,我无法发现它。
你能帮我推吗?

What am I missing here? I know that it is something small, but as a newbiew, I'm not able to spot it. Can yuo give me a push?

推荐答案

你没有遵守<$ c中的JavaFX特定访问器语法$ c>房间 datamodel:

You didn't obey JavaFX specific accessor syntax in your Room datamodel:

如果该字段为ObservableValue,则为:

If the field is ObservableValue like:

private StringProperty val = new SimpleStringProperty();

应该有以下访问者:

public String getVal() {
    val.get();
}

public void setVal(String newVal) {
    val.set(newVal);
}

public StringProperty valProperty() {
    return val;
}

注意方法val Property 。另请参阅此Q& A条目

Note the method valProperty. Also see this Q&A entry.

这篇关于为什么我的表中显示了StringProperty [value:&quot;&quot;]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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