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

标签 java mysql javafx scenebuilder

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

我有一个作业模型类,它具有优先级的 getter/setter 方法;

    public int getPrioritySetting() {
    return prioritySetting;
    }

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

我有两个问题,从 MySQL 数据库获取每个单独作业的优先级的“最简单”方法是什么,以及(使用这个)修改行外观的“最简单”方法是什么?我目前正在 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 代码。

关于java - 使用 MySQL 查询的结果更新 TableView 行颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32393238/

相关文章:

java - 在 xmpp 中创建所有聊天、群组消息和不同 pubsub 出版物的个人聚合

java - 登录网站到外部门户

Mysql 连接 对于每个应用程序,我尝试运行相同的错误代码

mysql - nrpe: 命令 mysql_check 未定义

java - 将 ListView 绑定(bind)到 ObservableSet

java - 获得焦点时如何选择 JFormattedTextField 中的所有文本?

java - LibGDX应用程序崩溃: "batch cannot be null"

php - Laravel 多对多查询关系与 where 子句

JavaFX GridPane 动态添加元素

数据库连接无法完成的 Java 登录方法