0

When I try to drag a popupwindow an exception appears:

java.lang.IllegalArgumentException: argument type mismatch

I designed the popwindow in SceneBuilder and added two methods for the anchorpane:

  1. setOnMousePressed -> returns the position of the popupwindow
  2. setOnMouseDragged ->returns the exception.

The first method works fine and the second not. As I can't find a solution although the topic has been covered, I would be very thankful for some help.

Code of the poupwindow:

private static double xOffset = 0; 
private static double yOffset = 0;

@FXML
void setOnMouseDragged(MouseDragEvent event) {
    Stage window=(Stage)((Node) event.getSource()).getScene().getWindow();
    window.setX(event.getScreenX() + xOffset);
    window.setY(event.getScreenY() + yOffset);
}

@FXML
void setOnMousePressed(MouseEvent event) { 
    Stage window=(Stage)((Node) event.getSource()).getScene().getWindow();
    xOffset = window.getX() - event.getScreenX();
    yOffset = window.getY() - event.getScreenY();
    System.out.println("setOnMouseDraggedx:"+xOffset+" yOffset:"+yOffset);
//this method works and prints out x:-419.0 yOffset:-31.0
}
Zephyr
  • 9,885
  • 4
  • 28
  • 63
Marcus
  • 11
  • 2

1 Answers1

0

The EventHandler of the Node#onMouseDragged property handles MouseEvents, not MouseDragEvents. The former cannot be cast to the latter.

onMouseDragged

public final ObjectProperty<EventHandler<? super MouseEvent>> onMouseDraggedProperty

Defines a function to be called when a mouse button is pressed on this Node and then dragged.

Using:

@FXML
void setOnMouseDragged(MouseEvent event) { /* code */ }

Should solve the IllegalArgumentException.


Note that setOnMouseDragged and setOnMousePressed are odd names for methods that don't set the event handlers, but instead actually handle their respective events.

Community
  • 1
  • 1
Slaw
  • 37,820
  • 8
  • 53
  • 80
  • Hello Slaw, I am sorry, but I think that this is exactly what I have in my code (see above). I imported javafx.scene.input.MouseDragEvent and javafx.scene.input.MouseEvent. – Marcus Jul 07 '19 at 08:12
  • Here is the stack trace: – Marcus Jul 07 '19 at 08:14
  • Yes... but my answer explains that `MouseDragEvent` is the _wrong_ type to use. – Slaw Jul 07 '19 at 08:14
  • setOnMouseDraggedx:-450.0 yOffset:-32.0 Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: argument type mismatch at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.reflect.misc.Trampoline.invoke(Unknown Source) ..... and so on – Marcus Jul 07 '19 at 08:27
  • Just to reiterate: `Node#onMouseDragged` uses a `MouseEvent` **not** a `MouseDragEvent`. – Slaw Jul 07 '19 at 08:34
  • Hello Slaw, thank you for your patience. I did not realize that I was using the wrong mouse event. Now it works. Thank you once again. Best regards from Hamburg, Marcus – Marcus Jul 07 '19 at 08:36