在 fx 中,不会将 mouseEvents 分派(dispatch)到禁用的节点,最后是一个演示行为的快速示例。
对于像我这样的 Swinger,这有点令人惊讶:在我的领域,事件已交付,目标(ui-delegate)的任务是决定是否应处理事件。事实上,最近 - 完全有效的 IMO - use-case of showing a tooltip over a disabled component 指出了这一点
从技术上讲,调度似乎在 Node 的一种实现方法中被切断了:
/**
* Finds a top-most child node that intersects the given ray.
*
* The result argument is used for storing the picking result.
*/
@Deprecated
public final void impl_pickNode(PickRay pickRay, PickResultChooser result) {
// In some conditions we can omit picking this node or subgraph
if (!isVisible() || isDisable() || isMouseTransparent()) {
return;
}
这似乎是在命中检测过程中被调用的。如果是这样,它就会真正深入肠子,没有太多机会进行调整。
问题:
- 我的代码有什么问题(很容易漏掉一些明显的东西 ;-)
- 以上真的是根本原因吗?
- 是否有任何可配置的选项来强制发送?如果是这样,怎么做?
- 行为规范在哪里?查看了教程/api 文档,但找不到任何内容。
代码示例:
package fx.control;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
* @author Jeanette Winzenburg, Berlin
*/
public class MouseEventDisabled extends Application {
private Parent getContent() {
Pane parent = new Pane();
Rectangle r = new Rectangle(100, 100, 200, 200);
r.addEventHandler(MouseEvent.ANY, event -> System.out.println(event));
CheckBox button = new CheckBox("rectangle disabled");
r.disableProperty().bind(button.selectedProperty());
parent.getChildren().addAll(r, button);
return parent;
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = getContent();
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch();
}
}
最佳答案
public final ReadOnlyBooleanProperty disabledProperty
表示该节点是否被禁用。如果 disable 在场景图中自身或其祖先之一上设置为 true,则节点将被禁用。 禁用的节点应该以不同的方式呈现自身以向用户指示其禁用状态。这种禁用的渲染取决于 Node.js 的实现。默认情况下,javafx.scene.shape 中包含的形状类不实现此类渲染,因此使用形状处理输入的应用程序必须自己实现适当的禁用渲染。但是,javafx.scene.control 中定义的用户界面控件将实现对禁用敏感的呈现。
默认值: 错误的 也可以看看: isDisabled(), setDisabled( boolean 值)
关于java - 如何允许调度鼠标事件以禁用节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24182802/