{"id":1218,"date":"2021-01-13T18:47:55","date_gmt":"2021-01-13T18:47:55","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=1218"},"modified":"2021-01-13T18:55:06","modified_gmt":"2021-01-13T18:55:06","slug":"modal-non-modal-dialogs","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/","title":{"rendered":"Modal &#038; Non-Modal Dialogs"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a21c46d62424\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a21c46d62424\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/#Modal_Dialogs\" >Modal Dialogs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/#Non-Modal_or_Modeless_Dialogs\" >Non-Modal (or Modeless) Dialogs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/#JDialog_Constructors\" >JDialog Constructors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/#The_setModal_Method\" >The&nbsp;setModal()&nbsp;Method<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/modal-non-modal-dialogs\/#Example\" >Example<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Modal_Dialogs\"><\/span>Modal Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ve probably experienced dialog boxes in Microsoft Windows, or some other windowing system.&nbsp; Typically, you click on a menu item in the menu bar, or perhaps a button somewhere in the GUI of an application, and up comes a dialog box for you to interact with.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Most often, when one of these dialog boxes comes up, you find that you can only interact with it, and cannot interact with the rest of the application.&nbsp; If you try to, say, click on the main window of your application while the dialog box is up, you&#8217;ll find that it doesn&#8217;t respond (except that the system may beep at you, to indicate that you&#8217;re trying to do something that isn&#8217;t allowed.&nbsp;&nbsp; This kind of dialog box is called a&nbsp;<em><strong>modal dialog<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The idea is that the application wants you to supply the information requested in the dialog before you are allowed to do anything else in the program.&nbsp; So it prevents you from interacting with the rest of the application, for that reason.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is implemented by the call to&nbsp;<code><strong>show<\/strong><\/code>&nbsp;(or&nbsp;<code><strong>setVisible(true)<\/strong><\/code>) the dialog.&nbsp; The system blocks on that call, and the call will not return until the dialog is dismissed (set non-visible).&nbsp; A blocking call is one that does not return until the operation&nbsp; it is blocking on is completed.&nbsp; While the call is blocked, events (such as mouse clicks, keystrokes, etc.) that are intended for the rest of the application are prevented from being delivered.&nbsp; Only those events which are intended for the dialog box are actually delivered.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Non-Modal_or_Modeless_Dialogs\"><\/span>Non-Modal (or Modeless) Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes, however, applications need to be able to bring up a dialog box and have it stay visible, while at the same time interacting with the rest of the application.&nbsp;&nbsp; Some applications, such as sophisticated graphical editors like Adobe PhotoShop, operate with several dialogs visible at any given time.&nbsp;&nbsp; In these applications, the user is expected to enter information into these dialogs to control what is happening during the design process.&nbsp;&nbsp; To be useful, such dialogs need to be non-modal (sometimes known as modeless).&nbsp; That is, they do&nbsp;<em>not<\/em>&nbsp;block on their&nbsp;<code><strong>show<\/strong><\/code>\/<code><strong>setVisible(true)<\/strong><\/code>&nbsp;calls, and they do not prevent events from being delivered to the rest of the application.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JDialog_Constructors\"><\/span>JDialog Constructors<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Several constructors for the&nbsp;<code><strong>JDialog<\/strong><\/code>&nbsp;class expect an argument,&nbsp;<code><strong>boolean modal<\/strong><\/code>, which specifies whether the&nbsp;<code><strong>JDialog<\/strong><\/code>&nbsp;will be brought up as a modal or non-modal dialog.&nbsp;&nbsp; For those constructors which do not specify this argument, the default is modal.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_setModal_Method\"><\/span>The&nbsp;<code>setModal()<\/code>&nbsp;Method<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>JDialog<\/strong><\/code>&nbsp;also has a method:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>public void setModal(boolean&nbsp;b)<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">(actually,&nbsp;<code><strong>JDialog<\/strong><\/code>&nbsp;inherits it from its superclass,&nbsp;<code><strong>Dialog<\/strong><\/code>)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can call this method to change the modal setting for a&nbsp;<code><strong>JDialog<\/strong><\/code>&nbsp;on the fly, before showing the dialog.&nbsp; Of course, once the dialog is visible, changing its modal setting does nothing until the dialog is dismissed and then shown again.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of a non-modal dialog.&nbsp; I&#8217;ve modified the <code><strong>AboutDialogTest<\/strong><\/code> class that I showed you earlier.&nbsp; There is one tiny change that I&#8217;ve made (towards the bottom), where I&#8217;ve simply invoked a <code><strong>JDialog<\/strong><\/code> constructor that accepts a modal argument, and hard-coded it to specify non-modal (<code><strong>false<\/strong><\/code>):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [21]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Container;\nimport java.awt.FlowLayout;\n\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\n\nimport javax.swing.JDialog;\nimport javax.swing.JFrame;\nimport javax.swing.JLabel;\nimport javax.swing.JMenu;\nimport javax.swing.JMenuBar;\nimport javax.swing.JMenuItem;\nimport javax.swing.SwingConstants;\n\nclass AboutDialog extends JDialog\n{\n  public AboutDialog(JFrame parent)\n  {\n    super(parent, &quot;AboutDialog&quot;, false); \/\/ Non-modal\n    \n    \/\/ Add components to the dialog&#039;s content pane\n    Container contentPane = getContentPane();\n    contentPane.setLayout(new FlowLayout());\n    JLabel label = new JLabel(\n            &quot;AboutDialogTest V1&quot;, SwingConstants.CENTER);\n    contentPane.add(label);\n    label = new JLabel(\n            &quot;by Bryan J. Higgs&quot;, SwingConstants.CENTER);\n    contentPane.add(label);\n    label = new JLabel(\n            &quot;October, 2001&quot;, SwingConstants.CENTER);\n    contentPane.add(label);\n    \n    setSize(150, 100);\n  }\n}\n\nclass AboutDialogFrame extends JFrame\n{\n  public AboutDialogFrame(String title)\n  {\n    setTitle(title);\n    setSize(300, 200);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    \n    \/\/ Create menu bar with menu\n    JMenu fileMenu = new JMenu(&quot;File&quot;);\n    fileMenu.add(&quot;New...&quot;);\n    fileMenu.add(&quot;Open...&quot;);\n    fileMenu.add(&quot;Save&quot;);\n    fileMenu.add(&quot;Save As...&quot;);\n    fileMenu.addSeparator();\n    fileMenu.add(&quot;Quit&quot;);\n    \n    JMenu helpMenu = new JMenu(&quot;Help&quot;);\n    JMenuItem aboutItem = new JMenuItem(&quot;About...&quot;);\n    aboutItem.addActionListener(new ActionListener()\n    {\n      public void actionPerformed(ActionEvent ev)\n      {\n        launchAboutDialog();\n      }\n    }\n    );\n    helpMenu.add(aboutItem);\n    \n    JMenuBar menuBar = new JMenuBar();\n    menuBar.add(fileMenu);\n    menuBar.add(helpMenu);\n    setJMenuBar(menuBar);\n  }\n  \n  private void launchAboutDialog()\n  {\n    AboutDialog dialog = new AboutDialog(this);\n    dialog.setVisible(true);\n  }\n}\n\npublic class AboutDialogNonModalExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    AboutDialogFrame frame = \n          new AboutDialogFrame(&quot;AboutDialogTest&quot;);\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">It produces the following results:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/AboutDialogModeless.gif\" alt=\"\" class=\"wp-image-1222\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:15px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, because the&nbsp;<code><strong>AboutDialog<\/strong><\/code>&nbsp;is now non-modal, you can create as many instances of it as you would like, because you can still interact with the application frame&#8217;s menu.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modal Dialogs You&#8217;ve probably experienced dialog boxes in Microsoft Windows, or some other windowing system.&nbsp; Typically, you click on a menu item in the menu bar, or perhaps a button somewhere in the GUI of an application, and up comes a dialog box for you to interact with. Most often, when one of these dialog [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":1152,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-1218","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Modal Dialogs You&#8217;ve probably experienced dialog boxes in Microsoft Windows, or some other windowing system.&nbsp; Typically, you click on a menu item in the menu bar, or perhaps a button somewhere in the GUI of an application, and up comes a dialog box for you to interact with. Most often, when one of these dialog&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=1218"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1218\/revisions"}],"predecessor-version":[{"id":1224,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1218\/revisions\/1224"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1152"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=1218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}