JavaFX - TableView - setItems:TableView类型中的方法setItems(ObservableList)不适用于参数(ObservableList)

时间:2017-08-02 16:11:51

标签: java javafx tableview scenebuilder

当我尝试将项目设置为tableView时,我遇到了一个问题,我使用的是SceneBuilder的信息。

Main.java:

public class Main extends Application {
private static Stage theStage;  
public static void main(String[] args) throws Exception {
    testDatas();
    launch();
}

public void start(Stage stage) throws IOException {
    theStage = stage;
    Group acteur = new Group();
    acteur.getChildren().add(
    FXMLLoader.load(getClass().getResource("views/options.fxml")));
    theStage.setTitle("Where's My Money");      
    Scene scene = new Scene(acteur, 1280.0, 720.0);
    theStage.setScene(scene);
    theStage.show();
}

public static void initialize() {
    launch();
}

public static void setScene(Group acteur, String titre) throws IOException {
    Scene scene = new Scene(acteur);

    theStage.setTitle(titre);
    theStage.setScene(scene);
    theStage.show();
}
}

视图/ ControllerOptions.class

public class ControllerOptions implements Initializable{

@FXML private TableView<?> TV_currency;
@FXML private TableColumn<Currency, String> TC_name;
@FXML private TableColumn<Currency, Double> TC_value;
private ObservableList<Currency> currencies = FXCollections.observableArrayList();

//FUNCTIONS
@Override
public void initialize(URL location, ResourceBundle rb){
    //initialisation Table Currencies
    for (Currency currency : Datas.getInstance().getCurrencies()) {
        currencies.add(currency);
    }       
    TC_name.setCellValueFactory(new PropertyValueFactory<Currency, String>("name"));
    TC_value.setCellValueFactory(new PropertyValueFactory<Currency, Double>("value"));
    TV_currency.setItems(currencies); // <= HERE'S THE ERROR
}
}

模型/ Currency.class

public class Currency {

private String name;
private double value;

public Currency(String name, double value) {
    setName(name);
    setValue(value);
}

public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}

public void setValue(double value) {
    this.value = value;
}
public double getValue() {
    return value;
}
}

我有这个错误: TableView类型中的方法setItems(ObservableList)不适用于参数(ObservableList)

如果你能帮助我,我将非常感激。

提前致谢

1 个答案:

答案 0 :(得分:2)

您正确地将支持数据声明为

private ObservableList<Currency> currencies ;

但您使用通配符声明了表:

@FXML private TableView<?> TV_currency;

因此,当您尝试在表格中设置项目时,类型不匹配。

将表的声明更改为

@FXML private TableView<Currency> TV_currency;