[英]How to disable mouse events over a certain area
So what I have is a smaller Pane inside a larger Pane, and what I want to do is enable/disable any handling of mouse events over that smaller pane. 因此,我在较大的窗格中拥有一个较小的窗格,而我想做的是启用/禁用该较小窗格上的鼠标事件的任何处理。 That would be done in theory by setting the mouse events of the smaller pane and all its children to null , and then restoring them later.
从理论上讲,这可以通过将较小窗格及其所有子窗格的鼠标事件设置为null ,然后在以后还原它们来完成。 But this is tedious and I was wondering if there is a simpler way.
但这很乏味,我想知道是否有更简单的方法。
Could I set a transparent Pane over that smaller pane to "capture" the mouse clicks for that area? 我可以在较小的窗格上设置一个透明窗格来“捕获”该区域的鼠标单击吗? Any suggestion is appreciated.
任何建议表示赞赏。 btw I am working in javafx.
顺便说一句,我在javafx中工作。
Use a event filter for the smallPane
and consume mouse events for smallPane
and it's decendants: 使用事件过滤器的
smallPane
和消费的鼠标事件smallPane
,它的decendants:
EventHandler<MouseEvent> handler = MouseEvent::consume;
// block events
smallPane.addEventFilter(MouseEvent.ANY, handler);
to reenable remove the event filter later you can do 要重新启用稍后删除事件过滤器,您可以执行
smallPane.removeEventFilter(MouseEvent.ANY, handler);
This way you disable just mouse events, not KeyEvent
s, ect... 这样,您仅禁用鼠标事件,而不禁用
KeyEvent
。
You can just disable the pane: 您可以仅禁用窗格:
smallPane.setDisable(true);
which will also disable any of its child nodes. 这也将禁用其任何子节点。 (See the documentation , which says "Setting disable to true will cause this
Node
and any subnodes to become disabled."). (请参阅文档 ,其中说:“将disable设置为true会导致该
Node
和所有子节点被禁用。”)。
To enable it again, just do 要再次启用它,只需执行
smallPane.setDisable(false);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.