2015-09-04 48 views
0

我的用戶可以創建添加到MySQL數據庫的作業。工作有優先權(1,2或3)。我想要做的是根據作業的優先級修改各行的顏色,例如prioirty 3是紅色的行,因爲這是一個更緊急的工作,優先級1是綠色的行,因爲它具有較低的緊迫性。使用MySQL查詢結果更新TableView行顏色

我有了對優先級的getter/setter工作模型類;

public int getPrioritySetting() { 
    return prioritySetting; 
    } 

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

我有兩個問題,什麼是「最簡單」的方式來獲得MySQL數據庫的每個inidivual作業的優先級和(使用本),什麼是「最簡單」的方法來修改的外觀行?我目前在JavaFX中使用通過scenebuilder構建的FXML文件的TableView。

回答

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); 
    } 
} 

與上面顯示的CSS代碼table-view-with-priority.css

+0

我需要將CSS文件應用於任何東西嗎?還是PseudoClass會自己挑選它? – jbanks

+0

您需要外部CSS文件(只需將其添加到場景的樣式表中)。你可以使用內聯CSS來實現它,但是如果你想改變樣式,它會變得更加混亂並且很難找到代碼,所以這更好。 –

+0

謝謝我現在有這個完美的工作。爲了擴大這一點,在評論中是否有足夠的空間來解釋我將如何使每個單元格的文本不論優先級如何都大膽?單元格上的顏色使得文本(已經很弱)現在難以閱讀。 – jbanks