JavaFX - TextArea,如何仅在双击时激活文本区域?
JavaFX - TextArea, how to activate the textarea only on double click?
JavaFX 2.2 - JDK1.8.0_121
我在一个矩形内有一个 TextArea,它也恰好有一个鼠标侦听器。问题是,当我点击 TextArea 时,它消耗了事件,而矩形没有得到点击。
例如考虑以下代码:
Group g = new Group();
Rectangle rect = new Rectangle(100,100);
TextArea textArea = new TextArea("Test");
textArea.setTranslateX(rect.getX());
textArea.setTranslateY(rect.getY());
textArea.setMinWidth(rect.getWidth());
textArea.setMinHeight(rect.getHeight());
//Calling a method to add an onMouseClickedProperty() mouse listener to the rectangle
addMouseListener(rect)
g.getChildren().addAll(rect, textArea);
在上面的例子中,TextArea 与矩形一样多 space 所以当我点击它时,onMouseClickedProperty() 事件被 TextArea 消耗。
有没有办法 "disable" 或 "remove" 来自 TextArea 的 onMouseClickedProperty() 而不是在双击发生时触发它?希望鼠标单击将被矩形消耗。
谢谢。
编辑:
我找到了一个可行的解决方案,它并不完美,但比评论中讨论的更合适。
由于您无法阻止 TextArea 使用 MOUSED_PRESSED 事件,因此在 TextArea 区域处理事件之前处理事件的唯一方法是使用事件过滤器。
所以使用上面的示例代码,我调用方法 addMouseListener(rect) 而不是只使用鼠标侦听器,我添加了一个事件过滤器,而不是添加我将它添加到组中。
private void addMouseLisenter(Group group){
group.addEventFilter(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
//Code here
}
});
}
这样组和 TextArea 都能得到鼠标点击。
注意:如果只希望组获得鼠标点击,可以添加event.consume()。
我希望这对以后寻找类似东西的人有所帮助。
我很确定你无法摆脱必须拥有 MouseListener 不幸的是因为它是所有鼠标事件的主要 class 如果你想让文本区域对鼠标做出反应它必须有听众。
就检测双击而言,还有另一个线程包含您正在寻找的答案 answered by Uluk Biy。
mipa 提出的另一个答案可能会回答您关于检测单击和双击之间的差异的问题,但是如果节点相互重叠。
编辑
也许在这种情况下,可能值得修改 mipa 的答案,尝试将其添加到您的代码中(在适用区域)
Integer clickCount = 0;
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ScheduledFuture<?> scheduledFuture;
root.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (e.getButton().equals(MouseButton.PRIMARY) && clickCount < 1) {
scheduledFuture = executor.schedule(() -> clickAction(), 500, TimeUnit.MILLISECONDS);
}
clickCount += 1;
}
});
private void clickAction() {
if (clickCount == 1) {
//actions for single click
clickCount = 0;
} else if (clickCount > 1) {
//action for multiple clicks
clickCount = 0;
}
}
我找到了比所讨论的更合适的解决方案,请检查我的问题中的编辑。
JavaFX 2.2 - JDK1.8.0_121
我在一个矩形内有一个 TextArea,它也恰好有一个鼠标侦听器。问题是,当我点击 TextArea 时,它消耗了事件,而矩形没有得到点击。
例如考虑以下代码:
Group g = new Group();
Rectangle rect = new Rectangle(100,100);
TextArea textArea = new TextArea("Test");
textArea.setTranslateX(rect.getX());
textArea.setTranslateY(rect.getY());
textArea.setMinWidth(rect.getWidth());
textArea.setMinHeight(rect.getHeight());
//Calling a method to add an onMouseClickedProperty() mouse listener to the rectangle
addMouseListener(rect)
g.getChildren().addAll(rect, textArea);
在上面的例子中,TextArea 与矩形一样多 space 所以当我点击它时,onMouseClickedProperty() 事件被 TextArea 消耗。
有没有办法 "disable" 或 "remove" 来自 TextArea 的 onMouseClickedProperty() 而不是在双击发生时触发它?希望鼠标单击将被矩形消耗。
谢谢。
编辑:
我找到了一个可行的解决方案,它并不完美,但比评论中讨论的更合适。
由于您无法阻止 TextArea 使用 MOUSED_PRESSED 事件,因此在 TextArea 区域处理事件之前处理事件的唯一方法是使用事件过滤器。
所以使用上面的示例代码,我调用方法 addMouseListener(rect) 而不是只使用鼠标侦听器,我添加了一个事件过滤器,而不是添加我将它添加到组中。
private void addMouseLisenter(Group group){
group.addEventFilter(MouseEvent.MOUSE_PRESSED,
new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
//Code here
}
});
}
这样组和 TextArea 都能得到鼠标点击。
注意:如果只希望组获得鼠标点击,可以添加event.consume()。
我希望这对以后寻找类似东西的人有所帮助。
我很确定你无法摆脱必须拥有 MouseListener 不幸的是因为它是所有鼠标事件的主要 class 如果你想让文本区域对鼠标做出反应它必须有听众。
就检测双击而言,还有另一个线程包含您正在寻找的答案 answered by Uluk Biy。
mipa 提出的另一个答案可能会回答您关于检测单击和双击之间的差异的问题,但是如果节点相互重叠。
编辑
也许在这种情况下,可能值得修改 mipa 的答案,尝试将其添加到您的代码中(在适用区域)
Integer clickCount = 0;
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ScheduledFuture<?> scheduledFuture;
root.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (e.getButton().equals(MouseButton.PRIMARY) && clickCount < 1) {
scheduledFuture = executor.schedule(() -> clickAction(), 500, TimeUnit.MILLISECONDS);
}
clickCount += 1;
}
});
private void clickAction() {
if (clickCount == 1) {
//actions for single click
clickCount = 0;
} else if (clickCount > 1) {
//action for multiple clicks
clickCount = 0;
}
}
我找到了比所讨论的更合适的解决方案,请检查我的问题中的编辑。