JavaFX - Modal Dialog

How to implement "Modal Dialog" in JavaFX ? Before going into details of implementation, lets first have a look at common requirement for "Modal Dialog".. It must block..

  • Mouse Events beyond the modal dialog
  • Key Events beyond the modal dialog
  • Execution of current Thread until dialog is disposed

JavaSE 6 allows to specify various types of modality as in ModalityType. We will try to implement behavior some where between Application and Document Modal :)

<script src=""></script><script src=""></script>

To launch click on above image or
Use of "Block EDT" may lead to deadlock and freeze application

Blocking Mouse Events is very easy, we just need to place a Rectangle whose width and height spans the entire Scene. Then set Node.blocksMouse attribute to true.

Key Events can be restricted if we could ensure that none of nodes other than child of Modal-Dialog receives focus. One way to achieve this is to disable default focus traversal. Get a list of all focus-traversable nodes in the dialog. We can set Node.focusTraversable attribute to false. Now add key-listeners to all nodes. Since focus traversal is disabled, the node will receive TAB key events. When the node receives TAB key event we can explicitly move the request to next node by invoking Node.requestFocus. Request focus for previous node for Shift + TAB key event. Please refer to FocusCycleRoot.fx for more information.

Now comes the interesting part - block execution of current thread - But JavaFX is executed on Event Dispatch Thread - EventQueue.isDispatchThread() will return true. So we cannot block this thread, as it will freeze the user interface. Assuming that the underline Toolkit of JavaFX is built on top of AWT Toolkit, we can use AWT APIs to get around this situation. Before blocking the current thread, we can push a new EventQueue instance into System-Event-Queue, so as to create an alternate path for dispatching events. After disposing the modal dialog, we can restore the original event queue using pop. Please refer to for more information.

Above solution is not stable enough and also not future proof. It assume that Toolkit is AWT and we have access to EventQueue (For which we need to sign the Applet), also the API may not be compatible with other profiles. In addition to this, pop API has tendency to create deadlock, there were attempts to fix this in various releases, but still doesn't provide stable behavior (especially when called from JavaFX).

So we may have to revisit the requirement - "block execution of current thread". Instead we can write the program in such a way that the display of MessageBox will be the last statement in current execution block. Thus we don't have to block the thread. Then MessageBox can handle redirection to other blocks of code by invoking functions. Thus MessageBox will act like a decision box. This approach will work with most cases and can emulate the "block thread" behavior. Also you don't have to sign the application. Please refer to MessageBox.fx for more information.

Also JFXtras has built in Dialog implementation which supports modality. Please refer to JFXtras for more information.

Please try it out and let me know feedback..

<script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src=""></script>


Thank U Rakesh for this tips, this tips useful for my new project.

Posted by Noushad T.M on April 05, 2010 at 08:58 AM IST #

Thanks Noushad.. :)

Posted by Rakesh Menon on April 05, 2010 at 10:46 AM IST #

Thank you, it is good.
but what about prevent dialog to go out of scene borders?

Posted by Yavuz on April 06, 2010 at 11:57 AM IST #

@Yavuz Thanks! Yes, we can do some more boundary checks for windowX, windowY in onMouseDragged of Window.fx before calling window.requestLayout()

Posted by Rakesh Menon on April 06, 2010 at 01:10 PM IST #

Hi Rakesh,
How to create MDI in JavaFX, Please help me

Posted by Noushad T.M on April 08, 2010 at 02:37 AM IST #

@Noushad Approach will be similar to one in Custom-Window post. If we are following Swing design, there will be a top level container - DesktopPane which will have a sequence of Window objects. We need to handle max & min restricting the size with in its DesktopPane. Hope it helps.. I'll try to put out some code..

Posted by Rakesh Menon on April 08, 2010 at 03:24 AM IST #

Thank, Rakesh.

I find solve for this problem with JFXtras from your post.

Posted by Mirovan on April 23, 2010 at 04:43 AM IST #

Hi Rakesh,
I use JFXtras library for show dialog with a custom scene (XDialog) but i can't hide close button you know how?
Thank's for all.

Posted by Thanan on April 28, 2010 at 10:55 AM IST #

Post a Comment:
Comments are closed for this entry.

The views expressed on this blog are my own and do not necessarily reflect that of my organization


« June 2016