gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:00:58 25 4
gpt4 key购买 nike

我的用户可以创建添加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com