nc404
nc404

Reputation: 154

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. 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.

Upvotes: 1

Views: 4030

Answers (2)

James_D
James_D

Reputation: 209330

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.").

To enable it again, just do

smallPane.setDisable(false);

Upvotes: 4

fabian
fabian

Reputation: 82461

Use a event filter for the smallPane and consume mouse events for smallPane and it's 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 KeyEvents, ect...

Upvotes: 3

Related Questions