Reputation: 437
I have two buttons within in a pane on the scene. I have added the event handler like this:
Button reset=new Button ("Reset");
reset.addEventHandler(MouseEvent.MOUSE_CLICKED,new EventHandler<MouseEvent>(){
public void handle(MouseEvent event){
System.out.println("reseting");
model.reset();
Play(stage);
}});
Button guess=new Button ("Guess");
guess.addEventHandler(MouseEvent.MOUSE_CLICKED,new EventHandler<MouseEvent>(){
public void handle(MouseEvent event){
ArrayList<Integer> FullGuess=new ArrayList<Integer>();
boolean condition=true;
for (int y:fullGuess){
if (y==0){
condition=false;
}
}
}});
How can I do the event handling part in another class rather than doing it directly in the class as I have done?
Upvotes: 1
Views: 1573
Reputation: 205875
Your fragment uses an anonymous class to handle events, but you can promote your anonymous handler to a member of the enclosing class or a separate class having package-private access. In either case, you'll want to pass the required List<Integer>
as a parameter.
List<Integer> fullGuess = new ArrayList<Integer>();
…
Button guess = new Button("Guess");
guess.addEventHandler(MouseEvent.MOUSE_CLICKED, new MyEventHandler(fullGuess));
In another file:
class MyEventHandler implements EventHandler<MouseEvent> {
private final List<Integer> fullGuess;
public MyEventHandler(List<Integer> fullGuess) {
this.fullGuess = fullGuess;
}
@Override
public void handle(MouseEvent event) {
boolean condition = true;
for (int y : fullGuess) {
…
}
}
}
Upvotes: 1