使用 MySQL 查询的结果更新 TableView 行颜色

Updating TableView Row Colour with Result From MySQL Query

我的用户可以创建添加到 MySQL 数据库的作业。这些作业具有优先级(1、2 或 3)。我想做的是根据工作的优先级修改各个行的颜色,例如优先级 3 是红色行,因为这是一项更紧急的工作,优先级 1 是绿色行,因为它的紧迫性较低。

我有一个工作模型 class,优先级为 getter/setter;

    public int getPrioritySetting() {
    return prioritySetting;
    }

public void setPrioritySetting(final int prioritySetting) {
    this.prioritySetting = prioritySetting;
    }

我有两个问题,从 MySQL 数据库中获取每个单独作业的优先级的 "easiest" 方法是什么以及(使用这个),"easiest" 方法是什么修改行的外观?我目前在 JavaFX 中使用 TableView 和通过 scenebuilder 构建的 FXML 文件。

我不明白第一个问题:假设您无论如何都会在某个时候从数据库中获取 Job 对象,因此您只需填充 prioritySetting 字段即可。

要更改行的外观,请使用行工厂,并设置一些 CSS 伪类

PseudoClass highPriority = PseudoClass.getPseudoClass("high-priority");
PseudoClass lowPriority = PseudoClass.getPseudoClass("low-priority");
table.setRowFactory(tv -> new TableRow<Job>() {
    @Override
    public void updateItem(Job item, boolean empty) {
        super.updateItem(item, empty);
        pseudoClassStateChanged(highPriority, item != null && item.getPrioritySetting() == 3);
        pseudoClassStateChanged(lowPriority, item != null && item.getPrioritySetting() == 1);
    }
});

然后只需在外部 CSS 文件中定义您需要的任何样式即可:

.table-row-cell:high-priority {
    -fx-background: red ;
}
.table-row-cell:low-priority {
    -fx-background: green ;
}

这是一个 SSCCE

import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ObservableValue;
import javafx.css.PseudoClass;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableViewWithPriorityRowColor extends Application {

    @Override
    public void start(Stage primaryStage) {
        TableView<Job> table = new TableView<>();
        table.getColumns().add(column("Name", Job::nameProperty));
        table.getColumns().add(column("Value", Job::valueProperty));
        table.getColumns().add(column("Priority", Job::priorityProperty));

        PseudoClass highPriority = PseudoClass.getPseudoClass("high-priority");
        PseudoClass lowPriority = PseudoClass.getPseudoClass("low-priority");
        table.setRowFactory(tv -> new TableRow<Job>(){
            @Override
            public void updateItem(Job job, boolean empty) {
                super.updateItem(job, empty);
                pseudoClassStateChanged(highPriority, job != null && job.getPriority() == 3);
                pseudoClassStateChanged(lowPriority, job != null && job.getPriority() == 1);
            }
        });

        table.getItems().addAll(createJobs());

        Scene scene = new Scene(new BorderPane(table), 800, 600);
        scene.getStylesheets().add("table-view-with-priority.css");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public List<Job> createJobs() {
        Random rng = new Random();
        return IntStream.rangeClosed(1, 40)
                .mapToObj(i -> new Job("Job "+i, i, rng.nextInt(3) + 1))
                .collect(Collectors.toList());

    }

    public static <S,T> TableColumn<S,T> column(String title, Function<S, ObservableValue<T>> property) {
        TableColumn<S,T> col = new TableColumn<>(title);
        col.setCellValueFactory(cellData -> property.apply(cellData.getValue()));
        return col ;
    }

    public static class Job {
        private final StringProperty name  = new SimpleStringProperty();
        private final IntegerProperty value = new SimpleIntegerProperty();
        private final IntegerProperty priority = new SimpleIntegerProperty();

        public Job(String name, int value, int priority) {
            setName(name);
            setValue(value);
            setPriority(priority);
        }

        public final StringProperty nameProperty() {
            return this.name;
        }

        public final String getName() {
            return this.nameProperty().get();
        }

        public final void setName(final String name) {
            this.nameProperty().set(name);
        }

        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        public final IntegerProperty priorityProperty() {
            return this.priority;
        }

        public final int getPriority() {
            return this.priorityProperty().get();
        }

        public final void setPriority(final int priority) {
            this.priorityProperty().set(priority);
        }


    }

    public static void main(String[] args) {
        launch(args);
    }
}

使用文件 table-view-with-priority.css.

上面显示的 CSS 代码