表格单元格Java FX 8中的TreeView [英] TreeView in a table cell Java FX 8

查看:340
本文介绍了表格单元格Java FX 8中的TreeView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UI中有一个TableView,它显示数据库中的用户列表。此表的一列是可编辑的,当启用编辑时,此特定列的单元格将成为树视图,列出可以选择的各种选项。

I have a TableView in my UI, it displays an user list from the database. One of the column of this table is editable, when editing is enabled the cell of this particular column becomes a treeview, listing various options which can be chosen.

只是为了澄清,我试图在表格单元格上实现一个Datepicker或类似颜色的功能,但我自己的项目列表作为一个树。

Just to clarify, I am trying to implement a Datepicker or a colorpicker like functionality on a table cell, but with my own list of items as a tree.

该表的定义如下

private TableView<User> userTable;

显示树视图的特定列定义如下

The particular column which displays a tree view is defined like this

private TableColumn<User, TreeView> col4;

我设置了这样的setcellFactory方法来显示树

I set a setcellFactory method like this to display the tree

col4.setCellFactory(new Callback<TableColumn<User,TreeView>, TableCell<User,TreeView>>() {

            @Override
            public TableCell<User, TreeView> call(
                    TableColumn<User, TreeView> param)
            {
                returns a comboboxtablecell which is filled with a tree
            }
    });

在表格的相应列中,单击单元格时,单元格显示一个组合框并且打开时的组合框显示树值。

In the table, in the corresponding column, when I click the cell, the cell shows a combo box and the combobox on opening up shows the tree value.

但是当单元格处于不可编辑状态时,我不知道应该如何使用字符串设置setcellValuefactory value

However when the cell is in non-editable state, I am not sure how I should set the setcellValuefactory with a string value

col4.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<User,TreeView>, ObservableValue<TreeView>>() {

        @Override
        public ObservableValue<TreeView> call(
                CellDataFeatures< User,TreeView> param)
        {
                     }
  }

当单元格处于不可编辑状态时,我想在树视图中显示一个值作为字符串。我不知道如何根据方法签名返回类型treeview的可观察值并仍然显示一个字符串。

I want to display a value which is inside the treeview as a string when the cell is in a non editable state. I am at a loss as to how to return an observablevalue of type treeview as per the method signature and still display a string.

推荐答案

你需要区分你的 TableView 数据(以及它的 TableColumn s)正在呈现,以及用于呈现这些数据的单元格。 cellValueFactory 是一个对象,它确定如何从整行的值中获取特定列的值(即数据)。 cellFactory 是一个对象,用于确定如何获取向用户显示数据的单元格。

You need to distinguish between the data that your TableView (and it's TableColumns) is presenting, and the cells that are used to present those data. The cellValueFactory is an object that determines how to get the value (i.e. data) for a particular column from the value for the entire row. The cellFactory is an object that determines how to get the cell that presents the data to the user.

任何时候你写的东西像 TableColumn< User,TreeView< ...>> 它(几乎总是)是一个错误。类型参数中的第一个类型是表的每一行中对象的类型 - 这里很好 - 第二种是显示的数据类型。 TreeView 不是数据类型:它是UI元素的类型,即用于显示数据的类型。

Any time you write something like TableColumn<User, TreeView<...>> it is (almost always) a mistake. The first type in the type parameters is the type of the object in each row of the table - this is fine here - and the second is the type of the data that is displayed. TreeView is not a data type: it's the type of a UI element, i.e. the type of something that is used to display the data.

所以你想要这些东西。 (你在解释你的数据模型时没有太具体 - 这很好 - 但这可能不太正确;它会给你这个想法。)

So you want something along these lines. (You haven't been too specific in explaining your data model - which is fine - but this might not be quite right; it will give you the idea though.)

TableView<User> userTable ;
TableColumn<User, Options> optionsCol ;

用户类将有 ObjectProperty< Options>

public class User {
    private final ObjectProperty<Options> options = new SimpleObjectProperty<>(this, "options", new Options());
    public final Options getOptions() {
        return options.get();
    }
    public final void setOptions(Options options) {
        this.options.set(options);
    }
    public ObjectProperty<Options> optionsProperty() {
        return options ;
    }

    // other properties, etc...
}

显然这取决于封装该列中显示的数据的Options类:

And obviously this depends on an Options class encapsulating the data displayed in that column:

public class Options {
    // properties etc
}

现在你只需使用默认的单元格值factory:

Now you just use the default cell value factory:

optionsCol.setCellValueFactory(new PropertyValueFactory("options"));

(或者在JavaFX 8中,我更喜欢

(or in JavaFX 8, I prefer

    optionsCol.setCellValueFactory((TableColumn.cellDataFeatures<User, Options> data) -> 
        data.getValue()    // this is the User object
        .optionsProperty() // this is an ObjectProperty<Options>, which is an ObservableValue<Options>
    );

这有点效率,因为它避免了 PropertyValueFactory 使用的反射,并且代码不多,特别是如果省略参数的类型lambda表达式。)

which is somewhat more efficient as it avoids the reflection that the PropertyValueFactory uses, and is not much more code, especially if you omit the type on the parameter to the lambda expression).

(你不需要像这样设置它。如果选项是不是用户的固有部分,那么你可以,例如,有一个 Map< User,ObjectProperty< Options>> 已定义并使用它来返回与每个用户关联的 ObjectProperty< Options> 。我展示的方式只是最多c ommon,也许最简单的方法。)

(You don't have to set it up like this. If the Options are not an intrinsic part of the User, then you could, for example, have a Map<User, ObjectProperty<Options>> defined and use that to return the ObjectProperty<Options> associated with each user. The way I showed is just the most common and probably easiest way.)

我真的不明白你的 ComboBox 适合哪里,但这个答案应该给你足够的工作,因为你需要它。 cellFactory 现在只需要返回 TableCell ,它使用 TreeView 显示选项对象:

I don't really understand where your ComboBox fits in, but this answer should give you enough to work that in as you need it. The cellFactory now just has to return a TableCell that uses a TreeView to display the Options object:

optionsCol.setCellFactory( col -> new TableCell<User, Options>() {
    private TreeView<...> treeView ;
    {
        treeView = new TreeView<>(...);
        // configure tree view, etc
    }
    @Override
    public void updateItem(Options options, boolean empty) {
        super.updateItem(options, empty) ;
        if (empty) {
            setGraphic(null);
        } else {
            // configure treeView with data from options, etc
            setGraphic(treeView);
        }
    }
});

你提到单元格是可编辑的,所以你需要连接单元格的所有编辑内容(你可能想要使用一个命名的[inner]类,因为它会变得有点冗长,而不是我在这里的匿名内部类)。但这应该给你基本的结构。

You mentioned the cell was editable, so you need to wire up all the editing stuff for the cell too (and you'll probably want to use a named [inner] class as it's going to get a bit verbose, instead of the anonymous inner class I have here). But this should give you the basic structure.

这篇关于表格单元格Java FX 8中的TreeView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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