This discussion is archived
5 Replies Latest reply: Feb 1, 2013 8:37 AM by anouard RSS

Best approach to manage overlay node?

anouard Newbie
Currently Being Moderated
Hi,
I need to overlay nodes. I tried with the simple example:
StackPane stack= new StackPane();
BorderPane under= new BorderPane();

BorderPane overlay = new BorderPane();
overlay.setTop(new Button("I'm over"));

stack.getChildren().addAll(under, overlay);
Unfortunately, the overlay BorderPane node capture all events. I'm looking for the best option for making it "transparent', only the button has to intercept the event.

Using JavaFX, what is the best approach to implement such a behavior?
Thanks.
  • 1. Re: Best approach to manage overlay node?
    David Grieve Pro
    Currently Being Moderated
    search the web for "javafx event filter"
  • 2. Re: Best approach to manage overlay node?
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. In javafx mouse events are only received by the top most node.
    There is also a method on Node: setMouseTransparent(boolean).
    Mouse events on node with mouseTransparent set to true will be ignored and captured by the topmost node below.
  • 3. Re: Best approach to manage overlay node?
    anouard Newbie
    Currently Being Moderated
    Thanks for the advices, unfortunately there's no method to remove event handler I didn't create. A Node.getHandlers() would be useful for removing built-in mouse handlers on Pane.

    Regarding setMouseTransparent(), this setting is inherited by all children node. In my use case, the Button will never receive the mouse event.

    To fix this in Swing, I would only override the contains(x,y) and return false if the mouse pointer is not over a valid component. I found the "equivalent" for JavaFX:
      
                @Override
                protected boolean containsBounds(double d, double d1) {
                     // If not over a Node, else return true
                       return false; 
                }
    But this method is deprecated, so my question is: Is it the good approach for jfx?
    Thanks
  • 4. Re: Best approach to manage overlay node?
    jsmith Guru
    Currently Being Moderated
    in Swing, I would only override the contains(x,y) and return false . . . I found the "equivalent" for JavaFX: But this method is deprecated . . . Is it the good approach for jfx?
    No. If you use deprecated methods you risk your code breaking in the future, and JavaFX is at a stage where deprecated methods are changed and removed all the time - so you don't really want to use them. A public API for mouse picking is on the future feature list for JavaFX, so it's almost guaranteed that deprecated stuff like this will change at some point.
    Regarding setMouseTransparent(), this setting is inherited by all children node. In my use case, the Button will never receive the mouse event.
    Use different layers and only set mouse transparent on each layer rather than on the layer container.

    For example:
    VBox layer1 = new VBox();
    layer1.getChildren().add(new Button("I am still clickable"));
    
    Node layer2 = new Circle(100, 100, 100, Color.FIREBRICK);
    layer2.setOpacity(0.7);
    layer2.setMouseTransparent(true);
    
    StackPane stack = new StackPane();
    stack.getChildren().setAll(layer1, layer2);
    I don't think it is applicable to what you want to do, but you should also be aware of the node.pickOnBounds method:
    http://docs.oracle.com/javafx/2/api/javafx/scene/Node.html#setPickOnBounds%28boolean%29

    Here is a some sample code which demonstrates the interaction between all of these things which I advise you study and run:
    https://gist.github.com/4601070
  • 5. Re: Best approach to manage overlay node?
    anouard Newbie
    Currently Being Moderated
    The magical answer to my needs was:
       myOverlayPane.setPickOnBounds(false);
    Then simply add your pane with a StakPane...
    I do like JavaFX!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points