繁体   English   中英

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

[英]Updating TableView Row Colour with Result From MySQL Query

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM