我有一个表,其中附有行工厂,以对某些行进行着色。问题在于,上下滚动表后,行只会更改颜色。表格启动时我该怎么做才能显示背景色?
此处有一个视频:https://screencast-o-matic.com/watch/cFQYlEqPSv
ObservableList<SupplierProductOTIF> orderData = FXCollections.observableArrayList();
orderTable.setRowFactory(row -> new TableRow<SupplierProductOTIF>() {
@Override
public void updateItem(SupplierProductOTIF item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setStyle("");
} else {
if (item.isException()) {
//We apply now the changes in all the cells of the row
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: rgba(255, 0, 0, .25);");
}
} else if (item.getQtyToOrder() > 0) {
//We apply now the changes in all the cells of the row
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: rgba(0, 255, 0, .25);");
}
} else {
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("");
}
setStyle("");
}
}
}
});
orderTable.setItems(orderData);
for (SupplierProductOTIF product : supplier.getOtifSupplierData()) {
if(product.getQtyToOrder() > 0 || product.isException()) {
orderData.add(0, product);
} else {
orderData.add(product);
}
}
编辑:这是我班上的完整代码
public class ReplenishmentPopupController extends ReportController implements Initializable {
private SupplierOTIF supplier;
private SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy");
@FXML
private TableView orderTable;
@FXML
private TableColumn productName;
@FXML
private TableColumn sku;
@FXML
private TableColumn currentStock;
@FXML
private TableColumn maxLeadTime;
@FXML
private TableColumn qtyToOrder;
@FXML
private TableColumn qtyOnOrder;
@FXML
private TableColumn nextOrderDate;
@FXML
private TableColumn<SupplierProductOTIF, LocalDate> stockUntilDate;
@FXML
private TableColumn daysStock;
@FXML
private TableColumn averageStockDailySales;
@FXML
private TableColumn daysOfStockToHold;
@FXML
private TableColumn daysOfStockToOrder;
@FXML
private TableColumn incost;
@FXML
private TableColumn supplierCost;
@FXML
private TableColumn orderCost;
@FXML
private TableColumn supplierPreference;
@FXML
private TableColumn productStatus;
@FXML
private TableColumn boxQty;
@FXML
private TableColumn minimumOrderQty;
@FXML
private TableColumn qtyRequired;
@FXML
private TableColumn rrs;
@FXML
private TableColumn rank;
@FXML
private Button raisePurchaseOrderButton;
@FXML
private Label supplierLabel;
@FXML
private Label orderValueLabel;
private ObservableList<SupplierProductOTIF> orderData = FXCollections.observableArrayList();
@Override
public void initialize(URL url, ResourceBundle rb) {
initOrderTable();
}
public void initOrderTable() {
orderTable.setEditable(true);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
sku.setCellValueFactory(new PropertyValueFactory("sku"));
sku.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
productName.setCellValueFactory(new PropertyValueFactory("name"));
productName.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.1));
currentStock.setCellValueFactory(new PropertyValueFactory("freeStock"));
currentStock.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
averageStockDailySales.setCellValueFactory(new PropertyValueFactory("averageForecastDailySales"));
averageStockDailySales.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
//averageStockDailySales.setVisible(false);
daysStock.setCellValueFactory(new PropertyValueFactory("daysStock"));
daysStock.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
stockUntilDate.setCellValueFactory(new PropertyValueFactory("stockUntilDate"));
stockUntilDate.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.1));
stockUntilDate.setCellFactory(tc -> new TableCell<SupplierProductOTIF, LocalDate>() {
@Override
protected void updateItem(LocalDate date, boolean empty) {
super.updateItem(date, empty);
if (empty || date == null) {
setText(null);
} else {
setText(formatter.format(date));
}
}
});
maxLeadTime.setCellValueFactory(new PropertyValueFactory("maxLeadTime"));
maxLeadTime.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
qtyToOrder.setCellValueFactory(new PropertyValueFactory("qtyToOrder"));
qtyToOrder.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.075));
qtyToOrder.setCellFactory(TextFieldTableCell.forTableColumn(new StringConverter<Integer>() {
@Override
public String toString(Integer object) {
return object.toString();
}
@Override
public Integer fromString(String string) {
return Integer.parseInt(string);
}
}));
qtyToOrder.setOnEditCommit(
new EventHandler<CellEditEvent<SupplierProductOTIF, Integer>>() {
@Override
public void handle(CellEditEvent<SupplierProductOTIF, Integer> t) {
((SupplierProductOTIF) t.getTableView().getItems().get(
t.getTablePosition().getRow())).setQtyToOrder(t.getNewValue());
updatePurchaseOrderValue();
}
});
daysOfStockToHold.setCellValueFactory(new PropertyValueFactory("daysOfStockRequired"));
daysOfStockToHold.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
qtyOnOrder.setCellValueFactory(new PropertyValueFactory("qtyOutStanding"));
qtyOnOrder.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.075));
//qtyOnOrder.setVisible(false);
minimumOrderQty.setCellValueFactory(new PropertyValueFactory("minimumOrderQty"));
minimumOrderQty.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
//minimumOrderQty.setVisible(false);
boxQty.setCellValueFactory(new PropertyValueFactory("boxQty"));
boxQty.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
qtyRequired.setCellValueFactory(new PropertyValueFactory("qtyRequired"));
qtyRequired.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
daysOfStockToOrder.setCellValueFactory(new PropertyValueFactory("daysOfStockToOrder"));
daysOfStockToOrder.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
//daysOfStockToOrder.setVisible(false);
incost.setCellValueFactory(new PropertyValueFactory("incost"));
incost.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
//incost.setVisible(false);
supplierCost.setCellValueFactory(new PropertyValueFactory("supplierCost"));
supplierCost.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
supplierCost.setVisible(false);
orderCost.setCellValueFactory(new PropertyValueFactory("orderCost"));
orderCost.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
//orderCost.setVisible(false);
supplierPreference.setCellValueFactory(new PropertyValueFactory("supplierPreference"));
supplierPreference.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
supplierPreference.setVisible(false);
productStatus.setCellValueFactory(new PropertyValueFactory("productStatus"));
productStatus.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
rrs.setCellValueFactory(new PropertyValueFactory("rank"));
rrs.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
rank.setCellValueFactory(new PropertyValueFactory("productRanking"));
rank.prefWidthProperty().bind(orderTable.widthProperty().multiply(0.05));
orderTable.setRowFactory(row -> new TableRow<SupplierProductOTIF>() {
@Override
public void updateItem(SupplierProductOTIF item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setStyle("");
} else {
if (item.isException()) {
//We apply now the changes in all the cells of the row
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: rgba(255, 0, 0, .25);");
}
} else if (item.getQtyToOrder() > 0) {
//We apply now the changes in all the cells of the row
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: rgba(0, 255, 0, .25);");
}
} else if (item.getQtyToOrder() == 0 && item.getFreeStock() <= 0) {
//We apply now the changes in all the cells of the row
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: rgba(0, 0, 255, .25);");
}
} else {
for (int i = 0; i < getChildren().size(); i++) {
((Labeled) getChildren().get(i)).setStyle("");
}
setStyle("");
}
}
}
});
}
public SupplierOTIF getSupplier() {
return supplier;
}
public void setSupplier(SupplierOTIF supplier) {
this.supplier = supplier;
}
public void calculatePurchaseOrder() {
supplier.calculateNumberOfDaysStock();
supplier.calculateQTYToOrder();
for (SupplierProductOTIF product : supplier.getOtifSupplierData()) {
if (product.getSupplierPreference() == 1) {
if (product.getQtyToOrder() > 0 || product.isException()) {
orderData.add(0, product);
} else {
orderData.add(product);
}
}
}
orderTable.setItems(orderData);
}
public void updatePurchaseOrderValue() {
double totalValue = 0.0;
for (SupplierProductOTIF product : orderData) {
totalValue += (product.getQtyToOrder() * product.getSupplierCost());
}
orderValueLabel.setText("Order Value: £" + SalesHelper.round(new BigDecimal(totalValue), 2, BigDecimal.ROUND_HALF_UP));
}
@Override
public void loadAndShowData() throws Exception {
calculatePurchaseOrder();
updatePurchaseOrderValue();
}
}