【问题标题】:JavaFX TableView Cell color change depending on text valueJavaFX TableView Cell 颜色根据文本值变化
【发布时间】:2018-02-17 04:26:15
【问题描述】:

我有一个带有 TableView 的 JavaFX 桌面应用程序。我使用名为 Orders 的 POJO 填充数据,该 POJO 最终来自 Firebird SQL 数据库。 Image of what I have now

我要做的是根据文本值更改第一列“状态”中每个单元格的背景填充颜色。因此,如果文本值为“READY”则为绿色,“STARTED”为黄色,“DONE”为灰色。 Image of what I would like

这是我用来填充 TableView 的代码部分:

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

我的 Orders POJO 的代码示例:

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

我尝试过使用回调,但我之前从未使用过回调,并且没有正确理解如何将我的需求融入到回调中。任何帮助对我的学习都很重要。谢谢。

【问题讨论】:

    标签: javafx tableview cell background-color


    【解决方案1】:

    您必须像这样为您的状态列定义一个自定义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)

    【讨论】:

      【解决方案2】:

      我终于找到了解决方案,无需使用任何额外的类,只需借助此 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);
          }
      `
      

      【讨论】:

        猜你喜欢
        • 2021-09-05
        • 1970-01-01
        • 2019-07-24
        • 2016-06-08
        • 2016-10-21
        • 2017-04-11
        • 2021-04-04
        • 2015-04-05
        • 1970-01-01
        相关资源
        最近更新 更多