Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Always set parent Frame for modal dialog

Though you can create a modal JDialog or JOptionPane without a parent frame (by passing null value as parent), avoid doing that. If the user minimizes the dialog, or uses ALT-tab to switch between applications, they might display an inactive window of your application. The window will not respond to any user action (because there is a modal dialog active), but the reasons will not be obvious (since modal dialog is not visible on the screen). To avoid this problem, and make your application more user friendly, always set the parent frame for modal JDialogs.

JDialog with a parent component will pop-up on the screen when the user activates that parent component, either with Alt-Tab or by clicking on the task bar button.

If you are displaying a dialog from an event handler, it might not be straight-forward what parameter to pass as the parent frame. Since you will mostly be using one instance of the “main frame” in applications, you can create a Singleton main frame class and getInstance() method for that class. Another solution is using a Singleton “Application” class, that has a reference to the currently active top-frame (see Use Singleton Application).


Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  best/always_set_parent_for_modal_dialogs.txt · Last modified: 2005/02/14 10:49
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: Statistics for project etracker� web controlling instead of log file analysis