javafx:表格行闪烁 [英] javafx: table row flashing
问题描述
对于每个交易
ReadOnlyBooleanProperty 名为警告
>我创建的对象。每个 Trade
对象都会在表视图中填充一行。现在,当小心
为
时,我希望表格行保持橙色闪烁。
I have a ReadOnlyBooleanProperty
named caution
for every Trade
object that I created. Each Trade
object populates a row in the table view. Now I want the table row to keep flashing in orange colour when caution
is true
.
public class Trade{
private DoubleProperty volume;
private ReadOnlyBooleanWrapper caution;
public Trade(double volume){
this.volume = new SimpleDoubleProperty(volume);
this.caution = new ReadOnlyBooleanWrapper();
this.caution.bind(this.volume.greaterThan(0));
}
}
如何保留表格行闪烁,只要警告
属性为真?
How can I keep the table row flashing forever as long as the caution
property is true?
推荐答案
要制作闪光灯,请使用 时间表
:
To make something flash, use a Timeline
:
Timeline flasher = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> {
// use "flash" color
}),
new KeyFrame(Duration.seconds(1.0), e -> {
// revert to regular color
})
);
在这种情况下更改颜色的最佳方法是使用 CSS PseudoClass
:
The best way to change the color in a case like this is to use a CSS PseudoClass
:
PseudoClass flashHighlight = PseudoClass.getPseudoClass("flash-highlight");
Node flashingNode = ... ;
Timeline flasher = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> {
flashingNode.pseudoClassStateChanged(flashHighlight, true);
}),
new KeyFrame(Duration.seconds(1.0), e -> {
flashingNode.pseudoClassStateChanged(flashHighlight, false);
})
);
flasher.setCycleCount(Animation.INDEFINITE);
然后在外部CSS文件中,您可以配置闪光突出显示的样式:
and then in an external CSS file you can configure the style for the flash highlight:
.node-type:flash-highlight {
/* style for flash "on" */
}
要将此绑定到布尔属性,只需使用属性创建一个侦听器:
To bind this to a boolean property, you just create a listener with the property:
someBooleanProperty.addListener((obs, oldValue, newValue) -> {
if (newValue) {
flasher.play();
} else {
flasher.stop();
flashingNode.pseudoClassStateChanged(false);
}
});
要将此应用于您的表行,您必须编写 rowFactory
。您只需要知道行中显示的项目可能会在行的生命周期内发生变化,因此您需要相应地更新状态和监听器:
To apply this to your table row, you have to write a rowFactory
. You just need to be aware that the item displayed in the row may change during the lifespan of the row, so you need to update the state and the listener accordingly:
TableView<Trade> table = ... ;
PseudoClass flashHighlight = PseudoClass.getPseudoClass("flash-highlight");
table.setRowFactory(tv -> {
TableRow<Trade> row = new TableRow<>();
Timeline flasher = new Timeline(
new KeyFrame(Duration.seconds(0.5), e -> {
row.pseudoClassStateChanged(flashHighlight, true);
}),
new KeyFrame(Duration.seconds(1.0), e -> {
row.pseudoClassStateChanged(flashHighlight, false);
})
);
flasher.setCycleCount(Animation.INDEFINITE);
ChangeListener<Boolean> cautionListener = (obs, cautionWasSet, cautionIsNowSet) -> {
if (cautionIsNowSet) {
flasher.play();
} else {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
}
};
row.itemProperty().addListener((obs, oldItem, newItem) -> {
if (oldItem != null) {
oldItem.cautionProperty().removeListener(cautionListener);
}
if (newItem == null) {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
} else {
newItem.cautionProperty().addListener(cautionListener);
if (newItem.cautionProperty().get()) {
flasher.play();
} else {
flasher.stop();
row.pseudoClassStateChanged(flashHighlight, false);
}
}
});
return row ;
});
然后只需定义一个类似
.table-row-cell:flash-highlight {
-fx-background: orange ;
}
以及您想要的其他任何款式。
and whatever other styles you want.
这篇关于javafx:表格行闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!