【发布时间】:2013-12-08 00:24:50
【问题描述】:
我正在尝试使用 JavaFX 创建一个“可拖动的”直方图 UI。我有一个 ScrollPane,其中包含一个 GridPane,它有 1 列和很多行。每行是一个包含标签的 HBox。每 10 行,还有一个包含 Line 的 HBox。
我尝试通过设置 onMousePressed、onMouseDragged 和 onMouseReleased 事件处理程序(如下所示)使包含线条的 HBoxes 可拖动。如果我在起点上方拖动并释放一条 hbox 线,它就会起作用 - 它最终会出现在我放入的任何网格行中,我可以再次单击并拖动它。但是,如果我在其起点下方拖动并释放一条线,我将无法为该 hBox 获取更多 mouseEvents。我尝试在任何地方添加日志语句,什么都没有。我试过设置onMouseOver,也没有触发。
为什么像这样在网格周围移动 hbox 可以向上拖动而不是向下拖动?
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... 处理程序并在每次释放鼠标时复制其子项来使其工作。但我仍然不知道是什么导致了原来的问题......
【问题讨论】:
标签: javafx