如何使用相同对象在类外调用一个方法。

huangapple go评论153阅读模式
英文:

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(&quot;Delete&quot;);
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&lt;ActionEvent&gt; {
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&lt;Food&gt; foodlist = null;
DataHandler() {
getFoodlist();
}
public ObservableList&lt;Food&gt; foodlist() {
return FXCollections.observableArrayList(getFoodlist());
}
private List&lt;Food&gt; getFoodlist() {
if (foodlist == null) {
foodlist = new ArrayList&lt;&gt;();
}
System.out.println(&quot;The List: &quot; + foodlist);
return foodlist;
}
public void createFood(Food food) {
System.out.println(&quot;createFood()&quot;);
this.foodlist.add(food);
update = true;
}
public void deleteFood(Food food) {
System.out.println(&quot;deleteFood()&quot;);
this.foodlist.remove(food);
update = true;
}
public void deleteFoodlist(List&lt;Food&gt; foodlist) {
System.out.println(&quot;deleteFoodlist()&quot;);
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());
}
} 
}
}

huangapple
  • 本文由 发表于 2020年7月29日 23:50:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63157502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定