有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javafx:表行闪烁

对于我创建的每个Trade对象,我都有一个名为cautionReadOnlyBooleanProperty。每个Trade对象在表视图中填充一行。现在,我希望表行在cautiontrue时保持橙色闪烁

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,我如何保持表行永远闪烁


共 (1) 个答案

  1. # 1 楼答案

    要使某些东西闪烁,请使用^{}

    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 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 {
        /* 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 ;
    });
    

    然后定义一个外部CSS文件,如下所示

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

    还有其他你想要的款式