英文:
How do i call a method outside a class with the same object
问题
以下是您要翻译的代码部分:
我有一个名为FoodManagerPane的类,它是由BorderPane扩展的。在这个类中,我使用不同的类组件构建我的面板。例如,我有一个名为MenuBar的类,在FoodManagerPane中进行初始化并插入到面板中。
**FoodManagerPane扩展了BorderPane**
public class FoodManagerPane extends BorderPane {
private MenuBarView menuBarView = new MenuBarView();
private Button buttonDelete = null;
...
private Button getButtonDelete() {
if (buttonDelete == null) {
buttonDelete = new Button("删除");
buttonDelete.setOnAction(deleteEvent);
}
return buttonDelete;
}
public void refreshData() {
this.getFoodTableView().setItems(DataHandler.INSTANCE.foodlist());
}
}
*DataHandler.INSTANCE.foodlist()* 返回列表的当前条目
在这个类中,除了其他方法之外,我还有一个名为*refreshData()*的方法,它将我的ObservableList中的数据与视图同步。
我还创建了一个用于从列表中删除对象的按钮。为此,我在一个单独的类中定义了ActionEvent。
**DeleteButton的ActionEvent**
public class DeleteFoodEventHandler implements EventHandler<ActionEvent> {
private final FoodTableView foodTableView = new FoodTableView();
@Override
public void handle(ActionEvent event) {
//做一些事情
foodTableView.setItems(DataHandler.INSTANCE.foodlist());
}
}
}
因此,在我的ActionEvent类中,我定义了应从列表中删除元素(这部分正常工作),但然后我想直接更新表格,不幸的是这里无法正常工作。
如果我在FoodManagerPane中直接定义ActionHandler并在这里调用*refreshData()*方法,它可以正常工作。但我想外包这个事件。这里有没有人有主意?
**更新:**
**DataHandler类**
public enum DataHandler {
INSTANCE;
private boolean update;
private List<Food> foodlist = null;
DataHandler() {
getFoodlist();
}
public ObservableList<Food> foodlist() {
return FXCollections.observableArrayList(getFoodlist());
}
private List<Food> getFoodlist() {
if (foodlist == null) {
foodlist = new ArrayList<>();
}
System.out.println("列表:" + foodlist);
return foodlist;
}
public void createFood(Food food) {
System.out.println("createFood()");
this.foodlist.add(food);
update = true;
}
public void deleteFood(Food food) {
System.out.println("deleteFood()");
this.foodlist.remove(food);
update = true;
}
public void deleteFoodlist(List<Food> foodlist) {
System.out.println("deleteFoodlist()");
for (Food food : foodlist) {
deleteFood(food);
}
}
请注意,代码中的注释也被翻译了。
英文:
I have a Classe FoodManagerPane that is extended by BorderPane. In this class I build my pane from different class components. For example, I have a class MenuBar, which I initialize in this FoodManagerPane and insert into the pane.
FoodManagerPane extends BorderPane
public class FoodManagerPane extends BorderPane {
private MenuBarView menuBarView = new MenuBarView();
private Button buttonDelete = null;
...
private Button getButtonDelete() {
if (buttonDelete == null) {
buttonDelete = new Button("Delete");
buttonDelete.setOnAction(deleteEvent);
}
return buttonDelete;
}
public void refreshData() {
this.getFoodTableView().setItems(DataHandler.INSTANCE.foodlist());
}
}
DataHandler.INSTANCE.foodlist() returns the current entries of the list
In this class I have among other things the method refreshData(), which synchronizes my data from the ObservableList with the view.
I also create a button to remove objects from my list. The ActionEvent for this I defined in a separate class.
ActionEvent for DeleteButton
public class DeleteFoodEventHandler implements EventHandler<ActionEvent> {
private final FoodTableView foodTableView = new FoodTableView();
@Override
public void handle(ActionEvent event) {
//do something
foodTableView.setItems(DataHandler.INSTANCE.foodlist());
}
}
}
So in my ActionEvent class I defined that the element should be removed from the list (that works) but then I want to update the table directly, which unfortunately is not working here.
If I define the ActionHandler directly in the FoodManagerPane and call the method refreshData() here it works. But I would like to outsource the event. Does anyone here have an idea?
UPDATE:
DataHandler-Class
public enum DataHandler {
INSTANCE;
private boolean update;
private List<Food> foodlist = null;
DataHandler() {
getFoodlist();
}
public ObservableList<Food> foodlist() {
return FXCollections.observableArrayList(getFoodlist());
}
private List<Food> getFoodlist() {
if (foodlist == null) {
foodlist = new ArrayList<>();
}
System.out.println("The List: " + foodlist);
return foodlist;
}
public void createFood(Food food) {
System.out.println("createFood()");
this.foodlist.add(food);
update = true;
}
public void deleteFood(Food food) {
System.out.println("deleteFood()");
this.foodlist.remove(food);
update = true;
}
public void deleteFoodlist(List<Food> foodlist) {
System.out.println("deleteFoodlist()");
for (Food food : foodlist) {
deleteFood(food);
}
}
答案1
得分: 0
这是解决方案,如果有人有类似的问题。非常感谢 @kendavidson
public class AddFoodEventHandler {
public void addFood(TableView tableView) {
try {
//做一些事情
} else {
// 做更多事情
// 刷新tableView
tableView.setItems(DataHandler.INSTANCE.foodlist());
}
}
}
英文:
This is the solution if someone has a similar problem. Many thanks to @kendavidson
public class AddFoodEventHandler {
public void addFood(TableView tableView) {
try {
//do something
} else {
// do more
// refresh the tableView
tableView.setItems(DataHandler.INSTANCE.foodlist());
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论