我的用户可以创建添加到MySQL数据库的作业。这些工作具有优先级(1,2或3)。我想要做的是根据作业的优先级修改单个行的颜色,例如prioirty 3是红色行,因为这是一个更紧急的工作,优先级1是绿色行,因为它具有较低的紧急程度。
我有一个工作模型类,它具有优先级的getter / setter;
public int getPrioritySetting() {
return prioritySetting;
}
public void setPrioritySetting(final int prioritySetting) {
this.prioritySetting = prioritySetting;
}
我有两个问题,"最简单的"从MySQL数据库中获取每个inidivual作业的优先级的方法和(使用这个)什么是最简单的"修改行的外观的方法?我目前在JavaFX中使用TableView和通过scenebuilder构建的FXML文件。
答案 0 :(得分:1)
我不明白第一个问题:大概是你在某个时候从数据库中获取了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代码。