Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2 event dispatching to underlying nodes

Is there a correct way to solve the problem with event propagation between two sibling panes?

For example we have StackPane with 2 panes inside.

StackPane p = new StackPane();
Region p1 = new Region();
Region p2 = new Region();
p.getChildren().addAll(p1, p2);

p2 in this example capture mouse events and p1 can't react on it even if event is not consumed.

Is there a correct way to propagate event to p1 if it not consumed by p2?

setMouseTransparent not solve my problem because I need that both children elements react on mouse.

Thanks for advise.

like image 483
Nik Avatar asked Mar 06 '13 13:03

Nik


People also ask

How events are processed in JavaFX?

In JavaFX applications, events are notifications that something has happened. As a user clicks a button, presses a key, moves a mouse, or performs other actions, events are dispatched. Registered event filters and event handlers within the application receive the event and provide a response.

What is JavaFX EventHandler?

The following article provides an outline for JavaFX EventHandler. In JavaFX, Event Handling is a process which controls an event as well as decides what has to be triggered, when an event occurs. This will be done writing a code called as an event handler that executes when an event occurs.

What is ActionEvent JavaFX?

An Event representing some type of action. This event type is widely used to represent a variety of things, such as when a Button has been fired, when a KeyFrame has finished, and other such usages.

Is JavaFX event driven?

GUI applications are event-driven. An application reacts to different event types which are generated during its lifetime. Events are generated by a user (a mouse click), an application (a timer), or the system (a clock).


2 Answers

By default events will just propagate up the heirarchy and terminate at the root. There are a few approaches you could take to solve your problem.

  1. Create your own event instance. Add an event handler to both regions that triggers your shared event instance. Add any event handling code you want to be common across regions to the shared instance. This is the approach I would take from the description you've given.
  2. Catch all events at the root and, instead of just letting them die, create a global event register that everyone can register for.
  3. Create an event handler at the first region and catches events and redispatches them at the second region (using buildEventDispatchChain.dispatchEvent). Then do the same on the other side.
like image 109
Pace Avatar answered Oct 26 '22 12:10

Pace


Just catch the event in an event handler and fire it on the other components:

top.addEventHandler(EventType.ROOT, event -> bottom.fireEvent(event));

You can still add mouse listeners on the top components and it works fine. If the bottom component does more fancy stuff with the event, you might need to clone and adjust it. This also works with more than two children.

like image 20
piegames Avatar answered Oct 26 '22 13:10

piegames