在javafx中填充tableview [英] Populate tableview in javafx

查看:536
本文介绍了在javafx中填充tableview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JavaFX中填充TableView.我的课程是Manual,它有另一个对象,如属性(User).如何使用User对象中的用户名填充表列.

I want to populate a TableView in JavaFX. My class is Manual and it has another object like attribute (User). How can I populate a table column with the username from the User object.

columnVanzator.setCellFactory(new Callback<TableColumn<Manual, User>, 
                                                        TableCell<Manual, User>>() {

   @Override
   public TableCell<Manual, User> call(TableColumn<Manual, User> arg0) {
        final TableCell<Manual, User> cell = new TableCell<Manual, User>() {
             @Override
             public void updateItem(final User item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        this.setText("");
                    } 
                    else {
                          this.setText(item.getUsername());
                    }
             }
         };
         return cell;
    } 
});

我在this.setText(item.getUsername());

推荐答案

您要查找的不是cellFactory,而是单元格 Value Factory. 如果您在列"中显示的类型比字符串或数字(例如,日期,州或什至更复杂的类型)更复杂,则使用自定义cellFactory.

What you're looking for is not a cellFactory, but a cellValueFactory. A custom cellFactory is used if you the type which is displayed in your Column is more complex than a String or a Number (e.g. a Date, State or even more complex types).

JavaFX的cellValueFactory与属性"一起使用效果最佳:

JavaFX' cellValueFactory works best with Properties:

column.setCellValueFactory((p) -> p.getValue().yourContentProperty());

但是看来您的对象中没有JavaFX属性,因此我们必须构建一些解决方法:

But it looks like you don't have a JavaFX Property for that in your Object, so we have to build a little workaround:

column.setCellValueFactory((p) -> new SimpleStringProperty(p.getValue().getUsername());

我建议您以JavaFX样式构建模型并在其中包含属性.如果值在属性内更改,则TableView将自动更新单元格. JavaFX中的几乎每个控件都一样.但是,如果您不能或根本不想,可以随意使用上面的方法,但是请记住,价值的变化不会自动通知视图!

I would recommend to build your Model in JavaFX-Style and have Properties inside it. If a value changes inside a Property, the TableView will update the Cell automatically. Same goes for almost every control in JavaFX. But if you don't can or simply don't want to, feel free to use the way above, but keep in mind that value changes won't automatically notify the view!

这篇关于在javafx中填充tableview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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