JavaFX-TableView-setItems : The method setItems(ObservableList) in the type TableView is not applicable for the arguments (ObservableList)

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

View /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)

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

提前致谢

最佳答案

您将支持数据正确声明为

private ObservableList<Currency> currencies ;

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

@FXML private TableView<?> TV_currency;

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

将表的声明更改为

@FXML private TableView<Currency> TV_currency;

关于JavaFX-TableView-setItems : The method setItems(ObservableList) in the type TableView is not applicable for the arguments (ObservableList),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45465944/

相关文章:

java - Eclipse 中的自动导入

java - 如何使用Java列表作为Hql插入的值?

java - 我如何从看起来像 json 字符串的字符串中获取所有键

javafx - 如何在 JFXtras Labs 8.0 中自定义窗口?

JavaFX:TableView 行选择

java - 了解 Future 的可中断取消

java - 获取 TextArea 边框和内部文本之间的像素

google-maps - 禁用谷歌地图瓷砖褪色或检测 map 是否完全呈现?

ios - 我如何从 UITableViewCell 获取信息?

ios - 对核心数据 UITableview 进行排序