我试图做的是将一个TableView中的选定行复制到另一个TableView中。JavaFX将数据从一个TableView传递到另一个TableView
public void addMeal()
{
products2 = FXCollections.observableArrayList();
tableProduct.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<productData>() {
@Override
public void changed(ObservableValue<? extends productData> observable, productData oldValue, productData newValue) {
products2.add(new productData(newValue.getName(), newValue.getKcal(), newValue.getProtein(), newValue.getCarb(), newValue.getFat()));
}
});
colProduct2.setCellValueFactory(new PropertyValueFactory<productData, String>("name"));
colKcal2.setCellValueFactory(new PropertyValueFactory<productData, String>("kcal"));
colProtein2.setCellValueFactory(new PropertyValueFactory<productData, String>("protein"));
colCarbs2.setCellValueFactory(new PropertyValueFactory<productData, String>("carb"));
colFat2.setCellValueFactory(new PropertyValueFactory<productData, String>("fat"));
tableProduct2.setItems(products2);
}
产品2是ObservableList,他们 tableProduct2是的TableView, colName2等都是TableColumn来
问题是,应用程序工作正常不。我必须先按下与此方法连接的按钮,然后在TableView 1中选择行之后,我才能使用TableView 2.另外,我可以单击其他几行,然后它们也将被添加。它就像这个按钮启用这个选择选项,我想要的只是选择行,单击按钮,在另一个TableView中看到这一行。然后我想选择另一个,再次点击按钮等。
接下来的问题是,一旦我再次单击按钮,它清除第二个TableView,从现在开始,他将添加选定的项目两次,如果我再次单击按钮,它再次清除TableView,现在它添加选定的项目3次,等等...