いずれかを使用EventHandler
ドラッグが発生した場合。例えば:
public class MyHandler implements EventHandler<MouseEvent> {
private final EventHandler<MouseEvent> onDraggedEventHandler;
private final EventHandler<MouseEvent> onClickedEventHandler;
private boolean dragging = false;
public Handler(EventHandler<MouseEvent> onDraggedEventHandler, EventHandler<MouseEvent> onClickedEventHandler) {
this.onDraggedEventHandler = onDraggedEventHandler;
this.onClickedEventHandler = onClickedEventHandler;
}
@Override
public void handle(MouseEvent event) {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED) {
dragging = false;
}
else if (event.getEventType() == MouseEvent.DRAG_DETECTED) {
dragging = true;
}
else if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
//maybe filter on dragging (== true)
onDraggedEventHandler.handle(event);
}
else if (event.getEventType() == MouseEvent.MOUSE_CLICKED) {
if (!dragging) {
onClickedEventHandler.handle(event);
}
}
}
}
次にボタンに追加します。
button.addEventHandler(MouseEvent.ANY,
new MyHandler(
e -> System.out.println("Dragged"),
e -> System.out.println("Clicked"))
);
*ラインのタイプミス(ドラッグすなわちとき、クリックし呼び出すことはありません) – sandboxj
[編集]タイプミスを修正するためにあなたの質問。 –