Java FX 8表行突出显示 [英] Java FX 8 table row highlighting
问题描述
我有一个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
- 定义一个
ObjectProperty<Person>
代表最近添加的人.在大多数情况下,这将是null
,但是当将新的Person
添加到列表时,它将被设置为该新的Person
:
- Define an
ObjectProperty<Person>
to represent the recently added person. Most of the time this will benull
but when a newPerson
is added to the list, it will be set to that newPerson
:
final ObjectProperty<Person> recentlyAddedPerson = new SimpleObjectProperty<>();
-
在表的项目列表中注册
ListListener
.将新项目添加到列表后,更新recentlyAddedPerson
.由于您不希望新人被无限期地标记为新人",因此请启动一个暂停过渡,该过渡将在一段时间(一两秒钟)后将recentlyAddedPerson
重置为null
.:
Register a
ListListener
with the table's items list. When a new item is added to the list, update therecentlyAddedPerson
. Since you don't want the new person to be labeled as "new" indefinitely, start a pause transition that will resetrecentlyAddedPerson
tonull
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屋!