JavaFX - TableView ProgressBar(错误 - 无法应用setCellValueFactory)

时间:2016-05-03 05:59:41

标签: progress-bar tableview javafx-8

使用FXML创建带有ProgressBar的TableView。我在下面的代码中收到包含ProgressBarTableCell的行的错误如下所示。我提到Link1& Link2但无法弄清楚我在做什么错误。

错误:

'setCellValueFactory(javafx.util.Callback<javafx.scene.control.TableColumn.CellDataFeatures<model.TableDisplayData,java.lang.Double>,javafx.beans.value.ObservableValue<java.lang.Double>>)' 

in 'javafx.scene.control.TableColumn' cannot be applied to

'(javafx.util.Callback<javafx.scene.control.TableColumn<model.TableDisplayData,java.lang.Double>,javafx.scene.control.TableCell<model.TableDisplayData,java.lang.Double>>)'

代码:

@FXML
private TableView<TableDisplayData> tvDisplay;
@FXML
private TableColumn<TableDisplayData,String> colCarModel;
@FXML
private TableColumn<TableDisplayData,String> colModelIndicator;
@FXML
private TableColumn<TableDisplayData,Double> colSpeedProgress;

@Override
public void initialize(URL location, ResourceBundle resources) {
colCarModel.setCellValueFactory(new PropertyValueFactory<TableDisplayData, String>("car"));
colModelIndicator.setCellValueFactory(new PropertyValueFactory<TableDisplayData, String>("indicator"));
colSpeedProgress.setCellValueFactory(new PropertyValueFactory<TableDisplayData, Double>("progressBar"));
colSpeedProgress.setCellValueFactory(ProgressBarTableCell.<TableDisplayData> forTableColumn());
}

TableDisplayData的代码:

public class TableDisplayData{
private String car;
private String indicator;
private DoubleProperty progressBar = new SimpleDoubleProperty();

public TableDisplayData(String car, String indicator, double progressBar) {
    this.car= car;
    this.indicator = indicator;
    setProgressBar(progressBar);
}
getters & setters....

1 个答案:

答案 0 :(得分:3)

您将cellFactorycellValueFactory混淆。 cellValueFactory告诉列要显示的数据。 cellFactory告诉列 如何显示数据。

所以你需要

colSpeedProgress.setCellValueFactory(new PropertyValueFactory<TableDisplayData, Double>("progressBar"));
colSpeedProgress.setCellFactory(ProgressBarTableCell.<TableDisplayData> forTableColumn());

(请注意,第二行更改为setCellFactory,而不是setCellValueFactory)。