Reputation: 159
I had very big eventhandling methods in my controller class. I placed each of them in a separate class but now they're not getting called anymore.
This is how it's currently implemented:
private DragDetectedHandler dragDetectedHandler = new DragDetectedHandler(stacks, nextCards, gameTable);;
node.setOnDragDetected(dragDetectedHandler);
If I call this it works:
node.setOnDragDetected(System.out::println);
(Just to test that the code is reached)
And the handler class:
public class DragDetectedHandler implements EventHandler<MouseEvent> {
private Stacks stacks;
private NextCards nextCards;
private Table gameTable;
public DragDetectedHandler(Stacks stacks, NextCards nextCards, Table gameTable) {
this.stacks = stacks;
this.nextCards = nextCards;
this.gameTable = gameTable;
}
@Override
public void handle(MouseEvent mouseEvent) {
System.out.println("Handle stuff here :)");
}
}
I'm probably overlooking something but similar questions didn't help me get my answer.
So the question: How should I call the event handler? Or why does the following code not call the event handler?
node.setOnDragDetected(dragDetectedHandler);
Upvotes: 1
Views: 2713
Reputation: 18592
the Node
should be enabled so that the event will work.
i tried this code , it's work when i drag the rectangle :
public class Main extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage stage) throws Exception
{
DragHandler dh = new DragHandler();
StackPane pane = new StackPane();
Rectangle rect = new Rectangle(200 , 200 , 200 ,200);
rect.setOnDragDetected(dh);
pane.getChildren().add(rect);
Scene scene = new Scene(pane , 600 , 600);
stage.setScene(scene);
stage.show();
}
private class DragHandler implements EventHandler<MouseEvent>
{
@Override
public void handle(MouseEvent event)
{
System.out.println("OK");
}
}
}
Upvotes: 1
Reputation: 106
I tried out this code and found it to work for me.
To try to find what could be wrong is you could check what you are importing and make sure it if javafx and not swing or awt.
Here is what I tried:
package test;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
public class DragDetectedHandler implements EventHandler<MouseEvent> {
private String test;
public DragDetectedHandler(String test) {
this.test = test;
}
@Override
public void handle(MouseEvent mouseEvent) {
System.out.println("Handle stuff here :)");
}
}
and the other class:
package test;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Test2 extends Application{
public static void main(String[] args) {
Test2.launch(args);
}
@Override
public void start(Stage stage) throws Exception {
DragDetectedHandler dragDetectedHandler = new
DragDetectedHandler("Test");
Button button = new Button();
button.setOnDragDetected(dragDetectedHandler);
Scene scene = new Scene(button);
stage.setScene(scene);
stage.show();
}
}
Upvotes: 2