javafx:表行闪烁
对于我创建的每个javafx:表行闪烁,java,javafx,Java,Javafx,对于我创建的每个Trade对象,我都有一个名为caution的ReadOnlyBooleanProperty。每个Trade对象在表视图中填充一行。现在,我希望当caution为true时,表行保持橙色闪烁 public class Trade{ private DoubleProperty volume; private ReadOnlyBooleanWrapper caution; public Trade(double volume){
Trade
对象,我都有一个名为caution
的ReadOnlyBooleanProperty
。每个Trade
对象在表视图中填充一行。现在,我希望当caution
为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));
}
}
只要
caution
属性为true,如何使表行始终闪烁?要使某些内容闪烁,请使用:
在这种情况下,更改颜色的最佳方法是使用:
然后在外部CSS文件中,您可以配置flash高亮显示的样式:
.node-type:flash-highlight {
/* style for flash "on" */
}
要将其绑定到布尔属性,只需使用以下属性创建一个侦听器:
someBooleanProperty.addListener((obs, oldValue, newValue) -> {
if (newValue) {
flasher.play();
} else {
flasher.stop();
flashingNode.pseudoClassStateChanged(false);
}
});
要将其应用于表行,必须编写一个rowFactory
。您只需注意,行中显示的项目可能会在行的生命周期内发生更改,因此需要相应地更新状态和侦听器:
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。setRowFactory(每当添加新行时突出显示行)
。由于存在多个setRowFactory
,后者总是覆盖前者。我怎么能同时让表格高亮显示和表格闪烁?很抱歉,也许我应该把它作为另一个问题发布。只需将所有逻辑包含在一个单行工厂中。我试过了。但我不断地发现语法错误。你能给我举个简单的例子吗?嗨,詹姆斯,因为这是一个新问题,我把它移到这里。
someBooleanProperty.addListener((obs, oldValue, newValue) -> {
if (newValue) {
flasher.play();
} else {
flasher.stop();
flashingNode.pseudoClassStateChanged(false);
}
});
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 ;
}