首页 > 解决方案 > 具有 CSS 滚动问题的 JavaFX tableview

问题描述

我目前面临 javafx 表的问题。我有一个显示主题列表的表格视图。每行的背景颜色取决于受试者是否能够注册。绿色背景的科目可以注册,粉红色背景的科目不能注册。滚动表格时会出现问题。

滚动前的 TableView

向下和向上滚动后的 TableView

滚动后,行的背景颜色发生了变化,绿色背景的主题可能会变成粉红色,反之亦然。这可以完美地工作,而无需在表格中添加 css。

我用来设置行背景颜色的代码

tblAvailableSubjects.setRowFactory((TableView<Subject> param) -> {
            TableRow<Subject> row = new TableRow<>();
            row.emptyProperty().addListener((obs, wasEmpty, isEmpty) -> {
                if(isEmpty) {
                    row.setContextMenu(null);
                    row.setStyle("-fx-border-color: transparent");
                } else {

                    Subject subject = row.getItem();

                    if(subject.getSubjectEvaluation().equals(SubjectEvaluation.COMPLETED)) {
                        row.setStyle("-fx-background: #B2EBF2");
                    } else if(subject.getSubjectEvaluation().equals(SubjectEvaluation.FAILED)) {
                        row.setStyle("-fx-background: #FF0000");
                        row.setContextMenu(tblAvailableContext);
                    } else if(subject.getSubjectEvaluation().equals(SubjectEvaluation.OKAY)) {
                        row.setStyle("-fx-background: #8BC34A");
                        row.setContextMenu(tblAvailableContext);
                    } else if(subject.getSubjectEvaluation().equals(SubjectEvaluation.ENROLLWITHCOREQ)) {
                        row.setStyle("-fx-background: #FFEB3B");
                        row.setContextMenu(tblAvailableContext);
                    } else if(subject.getSubjectEvaluation().equals(SubjectEvaluation.CANTENROLL)) {
                        row.setStyle("-fx-background: #FFCDD2");
                    }
                }
            });
            return row;
        });

表格的 CSS

.table-view {
    /*     Constants used throughout the tableview. */
    -fx-table-header-border-color: transparent;
    -fx-table-cell-border-color: -fx-box-border;
    /*    Horizontal Lines*/
    -fx-background-color: transparent;
}

.table-view .filler, .table-view .column-header
{
    -fx-size: 40;
    -fx-border-style: null;
    -fx-border-color: rgb(200.0, 200.0, 200.0);
    -fx-border-width: 0 0 1 0;
    -fx-background-color: transparent;
}

.table-view .show-hide-columns-button
{
    -fx-background-color: transparent;
}

.table-view .column-header .label,
.table-view .column-drag-header .label
{
    -fx-alignment: CENTER_LEFT;
}

.table-view .column-header-background
{
    -fx-background-color: transparent;
}

.table-row-cell {
    -fx-cell-size: 30px;
}

.table-cell {
    -fx-border-color: transparent;
    -fx-border-width: 1;
}

编辑:主题的 SubjectEvaluation 值没有改变,它似乎在滚动时在行之间切换上下文菜单和背景颜色。

我希望有人可以帮助我解决这个问题。谢谢。

标签: javafx

解决方案


推荐阅读