我正在嘗試使用JavaFX創建「可拖動」直方圖UI。我有一個ScrollPane,它包含一個GridPane,有1列和許多行。每一行都是一個包含標籤的HBox。每10行,還有一個包含Line的HBox。在GridPane中移動節點後JavaFX MouseEvent不會觸發
我試圖使含有通過設置onMousePressed,onMouseDragged和onMouseReleased事件處理程序(如下所示)可拖動線橫向方框。如果我在起始點之上拖放一個hbox-line,它就會起作用 - 它會在我放入的任何網格行中結束,我可以再次點擊並拖動它。但是,如果我在起點下方拖放一條線,我無法獲得該hBox的更多mouseEvent。我試圖在任何地方添加日誌語句,沒有任何東西我試圖設置小鼠過,它也沒有被解僱。
爲什麼會移動網格圍繞橫向盒這樣的工作拖了,但不下來?
lineContainer.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
EventTarget target = mouseEvent.getTarget();
lastY = mouseEvent.getSceneY();
}
});
lineContainer.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
Node target = (Node) mouseEvent.getTarget();
HBox hBox = null;
if (target instanceof HBox) {
hBox = (HBox) target;
}
else if (target instanceof Line) {
hBox = (HBox) target.getParent();
}
else { //should never happen
log.info("target not hbox or line: " + target.getClass());
}
if (mouseEvent.getSceneY() <= (lastY - 15)) {
int row = GridPane.getRowIndex(hBox);
GridPane.setRowIndex(hBox, --row);
lastY = mouseEvent.getSceneY();
lastRow = row - 1;
} else if (mouseEvent.getSceneY() >= (lastY + 15)) {
int row = GridPane.getRowIndex(hBox);
GridPane.setRowIndex(hBox, ++row);
lastRow = row - 1;
lastY = mouseEvent.getSceneY();
}
}
});
lineContainer.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mouseEvent) {
Node tar = (Node) mouseEvent.getTarget();
HBox hBox = null;
if (tar instanceof HBox) {
hBox = (HBox) tar;
}
else if (tar instanceof Line && tar.getParent() instanceof HBox) {
hBox = (HBox) tar.getParent();
}
else { //should never happen
log.info(mouseEvent.getTarget().getClass().toString());
}
}
});
更新:我設法得到它的每一個釋放鼠標時創建一個新的HBox中,重置onMouse ...處理器以及複製其兒童工作。但我仍然不知道是什麼導致了原來的問題...