javafx:表行闪烁

javafx:表行闪烁,第1张

javafx:表行闪烁

要使内容闪烁,请使用

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

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文件中,您可以配置Flash高亮显示的样式

.node-type:flash-highlight {    }

要将其绑定到布尔属性,只需使用该属性创建一个侦听器

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 ;});

然后只需定义一个外部CSS文件,例如

.table-row-cell:flash-highlight {    -fx-background: orange ;}

以及您想要的其他任何样式。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5487186.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存