Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javafx:表行闪烁_Java_Javafx - Fatal编程技术网

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