Java FX 8表行突出显示 [英] Java FX 8 table row highlighting

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

问题描述

我有一个Java FX8表,我使用一个可观察的列表填充了它.该表正在显示填充的数据.

I have a Java FX8 Table, I have populated it using an observable list. The table is displaying the populated data.

在用户交互过程中,创建了新行,我将此新数据添加到可观察列表中.该表将刷新.

During user interaction a new row is created, I add this new data to the observable list. The table gets refreshed.

我知道如何移动焦点以及如何滚动到此新添加的行.但是,我希望突出显示该行以显示它是新添加的.

I know how to move focus as well as to scroll to this newly added row. However I want this row to be highlighted to show that it is newly added.

如何获得对整个行作为节点的引用,以便可以使用该节点值突出显示该行.

How do I get a reference to the whole row as a Node, so that I can use this node value to highlight the row.

推荐答案

TableView上使用rowFactory会创建TableRow. TableRow是表示整个行的Node.有点棘手的部分是确定该行何时表示最近添加的行".

Use a rowFactory on the TableView which creates a TableRow. That TableRow is the Node representing the whole row. The slightly tricky part is identifying when the row represents a "recently added row".

我将按以下方式进行处理.我将使用常规联系表示例

I would approach this as follows. I'll use the usual contact table example

  1. 定义一个ObjectProperty<Person>代表最近添加的人.在大多数情况下,这将是null,但是当将新的Person添加到列表时,它将被设置为该新的Person:
  1. Define an ObjectProperty<Person> to represent the recently added person. Most of the time this will be null but when a new Person is added to the list, it will be set to that new Person:

final ObjectProperty<Person> recentlyAddedPerson = new SimpleObjectProperty<>();

  1. 在表的项目列表中注册ListListener.将新项目添加到列表后,更新recentlyAddedPerson.由于您不希望新人被无限期地标记为新人",因此请启动一个暂停过渡,该过渡将在一段时间(一两秒钟)后将recentlyAddedPerson重置为null.:

  1. Register a ListListener with the table's items list. When a new item is added to the list, update the recentlyAddedPerson. Since you don't want the new person to be labeled as "new" indefinitely, start a pause transition that will reset recentlyAddedPerson to null after some delay (a second or two).:

final Duration timeToGetOld = Duration.seconds(1.0);

table.getItems().addListener((Change<? extends Person> change) -> {
    while (change.next()) {
        if (change.wasAdded()) {
            List<? extends Person> addedPeople = change.getAddedSubList();
            Person lastAddedPerson = addedPeople.get(addedPeople.size()-1);
            recentlyAddedPerson.set(lastAddedPerson);

            // set back to null after a short delay, unless changed since then:
            PauseTransition agingTime = new PauseTransition(timeToGetOld);
            agingTime.setOnFinished(event -> {
                if (recentlyAddedPerson.get() == lastAddedPerson) {
                    recentlyAddedPerson.set(null);
                }
            });
            agingTime.play();
        }
    }
});

  • 为表创建行工厂.该行工厂返回自定义TableRow.此自定义TableRow创建一个BooleanBinding,如果该行表示最近添加的行,则将其设置为true. (如果该行的项目不为null且等于上面定义的recentlyAddedPerson,则为true.)

  • Create a row factory for the table. This row factory returns a custom TableRow. This custom TableRow creates a BooleanBinding which is set to true if this row represents a recently-added row. (This will be true if the row's item is not null and is equal to the recentlyAddedPerson defined above.)

    要真正实现突出显示,我只使用CSS PseudoClass.这样,突出显示的实现就变得微不足道了;只需将伪类状态设置为在TableRow实现中定义的BooleanBinding中的值即可.

    To actually implement the highlighting, I would just use a CSS PseudoClass. The implementation of the highlighting then becomes trivial; just set the pseudoclass state to the value in the BooleanBinding defined in the TableRow implementation.

    因此,您将需要以下内容:

    So you'll need something like:

        final PseudoClass newPersonPseudoClass = PseudoClass.getPseudoClass("new");
    

    rowFactory看起来像

        table.setRowFactory(tableView -> new TableRow<Person>() {
            // Bindings API uses weak listeners, so this needs to be a field to
            // make sure it stays in scope as long as the TableRow is in scope.
            private final BooleanBinding itemIsNewPerson 
                = Bindings.isNotNull(itemProperty())
                .and(Bindings.equal(itemProperty(), recentlyAddedPerson));
    
            {
                // anonymous constructor:
                itemIsNewPerson.addListener((obs, wasNew, isNew)
                        -> pseudoClassStateChanged(newPersonPseudoClass, isNew));
            }
        });
    

    最后,只需将以下CSS添加到样式表中即可:

    Finally, just add the following css to your stylesheet:

    .table-row-cell:new {
        -fx-background-color: darkseagreen ;
    }
    

    我张贴了完整示例作为要点.

    这篇关于Java FX 8表行突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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