2017-09-08 196 views
0

我有一個帶有TableView的JavaFX桌面應用程序。我使用名爲Orders的POJO填充數據,最終來自Firebird SQL數據庫。 Image of what I have nowJavaFX TableView根據文本值更改單元格顏色

我想要做的是根據文本值更改第一列'狀態'中每個單元格的背景填充顏色。所以如果文本值是'READY'然後是綠色,'STARTED'將是黃色的,'DONE'將是灰色的。 Image of what I would like

下面是我用填充TableView中的代碼部分:我的訂單POJO的

` 
@FXML private TableView<Orders> tblOrders; 
@FXML private TableColumn<Orders, Integer> clmStatus; 
@FXML private TableColumn<Orders, String> clmStartDateTime; 
@FXML private TableColumn<Orders, String> clmShopOrder; 
@FXML private TableColumn<Orders, String> clmRotation; 
@FXML private TableColumn<Orders, String> clmGMIECode; 
@FXML private TableColumn<Orders, String> clmSAPCode; 
@FXML private TableColumn<Orders, Integer> clmLineName; 
@FXML private TableColumn<Orders, Integer> clmOrderProductionNr; 
private ObservableList<Orders> list; 

public void initialize(URL location, ResourceBundle resources) { 
    populateTable(); 
} 

private void populateTable() { 
    log.appLog("Populating table\r\n"); 
    clmStatus.setCellValueFactory(new PropertyValueFactory<>("status")); 
    clmStartDateTime.setCellValueFactory(new PropertyValueFactory<> 
     ("startDateTime")); 
    clmShopOrder.setCellValueFactory(new PropertyValueFactory<>("extra1")); 
    clmRotation.setCellValueFactory(new 
     PropertyValueFactory<("batchLotNr")); 
    clmGMIECode.setCellValueFactory(new PropertyValueFactory<>("wareNr")); 
    clmSAPCode.setCellValueFactory(new PropertyValueFactory<> 
     ("serviceDescription")); 
    clmLineName.setCellValueFactory(new PropertyValueFactory<> 
     ("productionLineNr")); 
    clmOrderProductionNr.setCellValueFactory(new PropertyValueFactory<> 
     ("orderProductionNr")); 
    tblOrders.setItems(list); 
} 
` 

代碼示例:

` 
public class Orders { 
    private final SimpleStringProperty status; 
    private final SimpleStringProperty startDateTime; 
    private final SimpleStringProperty extra1; 
    private final SimpleStringProperty batchLotNr; 
    private final SimpleStringProperty wareNr; 
    private final SimpleStringProperty serviceDescription; 
    private final SimpleStringProperty productionLineNr; 
    private final SimpleIntegerProperty orderProductionNr; 

    Orders(String status, String startDateTime, String extra1, String batchLotNr, String wareNr, String serviceDescription, String productionLineNr, int orderProductionNr) { 
     this.status = new SimpleStringProperty(status); 
     this.startDateTime = new SimpleStringProperty(startDateTime); 
     this.extra1 = new SimpleStringProperty(extra1); 
     this.batchLotNr = new SimpleStringProperty(batchLotNr); 
     this.wareNr = new SimpleStringProperty(wareNr); 
     this.serviceDescription = new SimpleStringProperty(serviceDescription); 
     this.productionLineNr = new SimpleStringProperty(productionLineNr); 
     this.orderProductionNr = new SimpleIntegerProperty((orderProductionNr)); 
    } 

    public String getStatus() { 
     return status.get(); 
    } 

    public String getStartDateTime() {return startDateTime.get(); } 

    public String getExtra1() { 
     return extra1.get(); 
    } 

    public String getBatchLotNr() { 
     return batchLotNr.get(); 
    } 

    public String getWareNr() { 
     return wareNr.get(); 
    } 

    public String getServiceDescription() { 
     return serviceDescription.get(); 
    } 

    public String getProductionLineNr() { 
     return productionLineNr.get(); 
    } 

    int getOrderProductionNr() {return orderProductionNr.get();} 
} 
` 

我一直在使用一個回調試過,但我從來沒有使用回調之前和沒有正確理解我如何能夠滿足我的需求進入回調。任何幫助對我的學習都很重要。感謝所以。

回答

1

我終於找到了解決方案,而無需使用任何額外的課程,只是回調在我的控制器類的這種幫助的SO鏈接: StackOverFlow Link

` 
private void populateTable() { 
     log.appLog("Populating table\r\n"); 
     //clmStatus.setCellValueFactory(new PropertyValueFactory<>("status")); 

     clmStatus.setCellFactory(new Callback<TableColumn<Orders, String>, 
       TableCell<Orders, String>>() 
     { 
      @Override 
      public TableCell<Orders, String> call(
        TableColumn<Orders, String> param) { 
       return new TableCell<Orders, String>() { 
        @Override 
        protected void updateItem(String item, boolean empty) { 
         if (!empty) { 
          int currentIndex = indexProperty() 
            .getValue() < 0 ? 0 
            : indexProperty().getValue(); 
          String clmStatus = param 
            .getTableView().getItems() 
            .get(currentIndex).getStatus(); 
          if (clmStatus.equals("READY")) { 
           setTextFill(Color.WHITE); 
           setStyle("-fx-font-weight: bold"); 
           setStyle("-fx-background-color: green"); 
           setText(clmStatus); 
          } else if (clmStatus.equals("STARTED")){ 
           setTextFill(Color.BLACK); 
           setStyle("-fx-font-weight: bold"); 
           setStyle("-fx-background-color: yellow"); 
           setText(clmStatus); 
          } else if (clmStatus.equals("DONE")){ 
           setTextFill(Color.BLACK); 
           setStyle("-fx-font-weight: bold"); 
           setStyle("-fx-background-color: gray"); 
           setText(clmStatus); 
          } else { 
           setTextFill(Color.WHITE); 
           setStyle("-fx-font-weight: bold"); 
           setStyle("-fx-background-color: red"); 
           setText(clmStatus); 
          } 
         } 
        } 
       }; 
      } 
     }); 

     clmStartDateTime.setCellValueFactory(new PropertyValueFactory<>("startDateTime")); 
     clmShopOrder.setCellValueFactory(new PropertyValueFactory<>("extra1")); 
     clmRotation.setCellValueFactory(new PropertyValueFactory<>("batchLotNr")); 
     clmGMIECode.setCellValueFactory(new PropertyValueFactory<>("wareNr")); 
     clmSAPCode.setCellValueFactory(new PropertyValueFactory<>("serviceDescription")); 
     clmLineName.setCellValueFactory(new PropertyValueFactory<>("productionLineNr")); 
     clmOrderProductionNr.setCellValueFactory(new PropertyValueFactory<>("orderProductionNr")); 
     tblOrders.setItems(list); 
    } 
` 
0

你必須定義一個定製TableCell爲你這樣的狀態欄:

public class ColoredStatusTableCell extends TableCell<TableRow, Status> { 

    @Override 
    protected void updateItem(Status item, boolean empty) { 
     super.updateItem(item, empty); 
     if (empty || getTableRow() == null) { 
      setText(null); 
      setGraphic(null); 
     } else { 
      TableRow row = (TableRow) getTableRow().getItem(); 
      setText(item.toString()); 
      setStyle("-fx-background-color: " + row.getColorAsString()); 
      // If the statis is changing dynamic you have to add the following: 
      row.statusProperty() 
       .addListener((observable, oldValue, newValue) -> 
         setStyle("-fx-background-color: " + row.getColorAsString())); 
     } 
    } 
} 

其中TableRow

public class TableRow { 

    private ObjectProperty<Status> status; 
    private Map<Status, Color> statusColor; 

    public TableRow(Status status, Map<Status, Color> statusColor) { 
     this.status = new SimpleObjectProperty<>(status); 
     this.statusColor = statusColor; 
    } 

    public Status getStatus() { 
     return status.get(); 
    } 

    public ObjectProperty<Status> statusProperty() { 
     return status; 
    } 

    public Color getStatusColor() { 
     return statusColor.get(status.get()); 
    } 

    public String getColorAsString() { 
     return String.format("#%02X%02X%02X", 
       (int) (getStatusColor().getRed() * 255), 
       (int) (getStatusColor().getGreen() * 255), 
       (int) (getStatusColor().getBlue() * 255)); 
    } 
} 

狀態:

public enum Status { 
    READY, STARTED, DONE 
} 

和控制器:

public class TestController { 

    @FXML 
    private TableView<TableRow> table; 
    @FXML 
    private TableColumn<TableRow, Status> column; 

    private ObservableList<TableRow> data = FXCollections.observableArrayList(); 

    @FXML 
    public void initialize() { 
     column.setCellValueFactory(data -> data.getValue().statusProperty()); 
     column.setCellFactory(factory -> new ColoredStatusTableCell()); 
     Map<Status, Color> statusColor = new HashMap<>(); 
     statusColor.put(Status.READY, Color.GREEN); 
     statusColor.put(Status.STARTED, Color.YELLOW); 
     statusColor.put(Status.DONE, Color.GRAY); 

     TableRow ready = new TableRow(Status.READY, statusColor); 
     TableRow started = new TableRow(Status.STARTED, statusColor); 
     TableRow done = new TableRow(Status.DONE, statusColor); 

     data.addAll(ready, started, done); 

     table.setItems(data); 
    } 

} 

我選擇設置狀態爲enum因爲它更容易處理它, 然後我使用了一個映射到每個狀態色組合,然後在單元格可以將其背景色設置爲匹配的顏色狀態。

如果你想當然Color.YELLOW代替等等,你可以使用自定義Color.rgb(red,green,blue)