Cancel mode event to hide contextmenus?

Werner Lehmann lehmann at
Tue Feb 19 04:08:40 PST 2013


I am still having a hard time to make sure that contextmenus are hidden 
when the user moves or resizes the window underneath. This is on a 
JFXPanel, not sure if it is a problem in a pure FX application, too.

Currently this requires quite some code, see below for a snippet. And 
this works only if I actually have a reference to all contextmenus which 
could possibly be showing on a scene. Which is also a problem because on 
MenuButton and other controls the contextmenu is an implementation 
detail and it is intentionally hard or impossible to get a reference to 

On Win32 there is a WM_CANCELMODE event sent to controls which stops 
editing, unreleases mouse captures, and also closes menus. Is there 
something similar in FX which I could send to a root container to 
achieve this on every child control?


PS: for your "amusement", this is what I am currently doing to hide the 
menu IF I have a reference to it:

1. Wait until control is added to scene.
2. Wait until scene has a window.
3. Listen to changes on the window x/y/width properties to hide the 
contextmenu (don't need to listen to height property in this case).

>     sceneProperty().addListener(new InvalidationListener() {
>       @Override
>       public void invalidated(Observable arg0)
>       {
>         Scene s = getScene();
>         if (s != null)
>         {
>           s.windowProperty().addListener(new InvalidationListener() {
>             @Override
>             public void invalidated(Observable arg0)
>             {
>               if (getScene() != null && getScene().getWindow() != null)
>               {
>                 ChangeListener<Number> changeListener = new ChangeListener<Number>() {
>                   @Override
>                   public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue)
>                   {
>                     cmUser.hide();
>                   }
>                 };
>                 getScene().getWindow().xProperty().addListener(changeListener);
>                 getScene().getWindow().yProperty().addListener(changeListener);
>                 getScene().getWindow().widthProperty().addListener(changeListener);
>               }
>             }
>           });
>         }
>       }
>     });

More information about the openjfx-dev mailing list