【发布时间】:2017-04-16 17:29:53
【问题描述】:
我想要做的是将一个 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);
}
products2 是 ObservableList, tableProduct2 是 TableView, colName2 等是 TableColumns
问题是,应用程序运行不正常。我必须先按下与此方法相关的按钮,然后在 TableView 1 中选择行后,我进入 TableView 2。此外,我可以单击其他几行,它们也会被添加。它就像这个按钮启用了这个选择选项,我想要的是简单地选择行,单击按钮,在另一个 TableView 中查看这一行。然后我想再选择一个,再次点击按钮,等等。
下一个问题是,一旦我再次单击按钮,它会清除第二个 TableView,从现在开始,他将添加两次选定的项目,如果我再次单击按钮,它会再次清除 TableView,现在它会添加 3 次选定的项目,等等...
【问题讨论】: