{"id":1152,"date":"2021-01-13T17:05:21","date_gmt":"2021-01-13T17:05:21","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=1152"},"modified":"2021-01-13T17:46:28","modified_gmt":"2021-01-13T17:46:28","slug":"dialog-boxes","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/","title":{"rendered":"Dialog Boxes"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a6eb865c73e0\" 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-6a6eb865c73e0\"  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\/#JOptionPane_Dialogs\" >JOptionPane Dialogs<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/#Message_Dialogs\" >Message Dialogs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/#Confirm_Dialogs\" >Confirm Dialogs<\/a><\/li><\/ul><\/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\/#Option_Dialogs\" >Option Dialogs<\/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\/#Example\" >Example:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/swing-gui-components\/dialog-boxes\/#Getting_More_Sophisticated\" >Getting More Sophisticated<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">Then there are <em>dialog boxes<\/em>, of various different types&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"JOptionPane_Dialogs\"><\/span>JOptionPane Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>JOptionPane<\/strong><\/code>&nbsp;class provides a useful set of dialog boxes, from the very simple to the fairly complex.&nbsp; It&#8217;s surprising how much you can accomplish using just one of the static methods provided by&nbsp;<code><strong>JOptionPane<\/strong><\/code>.&nbsp; You can create:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>Message Dialog<\/em>&nbsp;&#8212; Tells the user about something that has happened.&nbsp;<\/li><li><em>Confirm Dialog<\/em>&nbsp;&#8212; Asks a confirming question, like yes\/no\/cancel.&nbsp;<\/li><li><em>Input Dialog<\/em>&nbsp;&#8212; Prompts for some input.&nbsp;<\/li><li><em>Option Dialog<\/em>&nbsp;&#8212; The Grand Unification of the above three.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Message_Dialogs\"><\/span>Message Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how you can create a <em>message dialog<\/em>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,11,12,13]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleMessage1\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showMessageDialog(\n        null, \/\/ The parent frame\n        &quot;You are hereby informed of an important fact.&quot; \n              \/\/ A message\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"329\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage1.gif\" alt=\"\" class=\"wp-image-1156\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Note that there are three obvious components of this dialog:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The message<\/li><li>An icon to the left of the message, indicating what kind of message it is (Informational, Warning, Error, etc.)<\/li><li>The OK button.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If we modify this simple example a little:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,11,12,13,14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleMessage2\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showMessageDialog(\n        null,     \/\/ The parent frame\n        &quot;You are hereby informed of an error.&quot;, \n                  \/\/ a message\n        &quot;Error&quot;,  \/\/ a title\n        JOptionPane.ERROR_MESSAGE \n                  \/\/ The message type\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">The result looks like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"278\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage2.gif\" alt=\"\" class=\"wp-image-1157\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:25px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Note two things:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The titlebar shows the specified title<\/li><li>The icon has changed to indicate that an error is indicated.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If we wanted to show a warning:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,11,12,13,14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleMessage3\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showMessageDialog(\n        null, \/\/ The parent frame\n        &quot;You are hereby warned.&quot;, \n              \/\/ a message\n        &quot;Warning&quot;, \/\/ a title\n        JOptionPane.WARNING_MESSAGE \n              \/\/ Warning type\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">it would look like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"268\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage3.gif\" alt=\"\" class=\"wp-image-1158\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:17px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You also have the option of not showing an icon:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,11,12,13,14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleMessage4\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showMessageDialog(\n        null, \/\/ The parent frame\n        &quot;This is a pretty plain message.&quot;, \n              \/\/ a message\n        &quot;Plain Message&quot;, \/\/ a title\n        JOptionPane.PLAIN_MESSAGE \n              \/\/ Plain type\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which looks like:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage4.gif\" alt=\"\" class=\"wp-image-1159\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:11px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">You can also ask a question:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,11,12,13,14,15,16]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleMessage5\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showMessageDialog(\n        null, \/\/ The parent frame\n        &quot;Are you sure you want to do this?&quot;, \n              \/\/ a message\n        &quot;Just checking...&quot;, \/\/ a title\n        JOptionPane.QUESTION_MESSAGE \n              \/\/ Question type\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which looks like this:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage5.gif\" alt=\"\" class=\"wp-image-1160\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:14px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Of course, when the only option is <strong>OK<\/strong>, it&#8217;s not terribly useful!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s where&nbsp;<em>Confirm Dialogs<\/em>&nbsp;come in&#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Confirm_Dialogs\"><\/span>Confirm Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s not very nice to ask a question, and then only give one option for an answer.&nbsp; So there&#8217;s also a <em>Confirm Dialog<\/em>, where we can specify more response buttons be made available:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleConfirm1\n{\n  public static void main(String&#x5B;] args)\n  {\n    JOptionPane.showConfirmDialog(\n        null, \/\/ The parent frame\n        &quot;Are you sure you want to do this?&quot;, \n              \/\/ a message\n        &quot;Just checking...&quot;, \/\/ a title\n        JOptionPane.YES_NO_OPTION, \n                      \/\/ The option type\n        JOptionPane.QUESTION_MESSAGE \n                      \/\/ the message type\n        );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleMessage5-1.gif\" alt=\"\" class=\"wp-image-1167\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:14px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Now, we&#8217;d like to know how the user answered, so we can do the following:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,19,20,21,22,23,24,25,26]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleConfirm2\n{\n  public static void main(String&#x5B;] args)\n  {\n    int status = JOptionPane.showConfirmDialog(\n        null, \/\/ The parent frame\n        &quot;Are you sure you want to do this?&quot;, \n              \/\/ a message\n        &quot;Just checking...&quot;, \/\/ a title\n        JOptionPane.YES_NO_OPTION, \n                      \/\/ The option type\n        JOptionPane.QUESTION_MESSAGE \n                      \/\/ the message type\n        );\n    if (status == JOptionPane.YES_OPTION)\n    {\n      System.out.println(&quot;You said Yes!&quot;);\n    }\n    else if (status == JOptionPane.NO_OPTION)\n    {\n      System.out.println(&quot;You said No!&quot;);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces the following, after the user responds by clicking on the <strong>No<\/strong> button:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>You said No!<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now, sometimes it&#8217;s necessary to give the user a third choice:&nbsp; <strong>Yes<\/strong>, <strong>No<\/strong>, or <strong>Cancel<\/strong>, so here&#8217;s how you would do that:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [14,23,24]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class SimpleConfirm3\n{\n  public static void main(String&#x5B;] args)\n  {\n    int status = JOptionPane.showConfirmDialog(\n        null, \/\/ The parent frame\n        &quot;Are you sure you want to do this?&quot;, \n              \/\/ a message\n        &quot;Just checking...&quot;, \/\/ a title\n        JOptionPane.YES_NO_CANCEL_OPTION, \n              \/\/ The option type\n        JOptionPane.QUESTION_MESSAGE \n              \/\/ the message type\n        );\n    if (status == JOptionPane.YES_OPTION)\n       System.out.println(&quot;You said Yes!&quot;);\n    else if (status == JOptionPane.NO_OPTION)\n       System.out.println(&quot;You said No!&quot;);\n    else if (status == JOptionPane.CANCEL_OPTION)\n       System.out.println(&quot;You cancelled!&quot;);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"122\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/SimpleConfirm2.gif\" alt=\"\" class=\"wp-image-1168\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">and in response to the user clicking on <strong>Cancel<\/strong>, the program prints out:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>You cancelled!<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Option_Dialogs\"><\/span>Option Dialogs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, there&#8217;s the&nbsp;<em>Option Dialog<\/em>, which has lots (lots!) of flexibility.&nbsp;&nbsp;<\/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<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [26,27,28,29,39,40]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JOptionPane;\n\npublic class TriviaGame1\n{\n  public static void main(String&#x5B;] args)\n  {\n    boolean playAgain = true;\n    while (playAgain)\n    {\n      playAgain = play();\n    }\n  }\n  \n  private static boolean play()\n  {\n    int score = 0;\n    String&#x5B;] choices = null;\n    int answer = -1;\n    int selection = JOptionPane.CLOSED_OPTION;\n    \n    \/\/ Questions 1 through 3...\n    \n    \/\/ Question 4 for sports\n    choices = new String&#x5B;]\n    {\n      &quot;USC&quot;, &quot;Notre Dame&quot;, &quot;North Carolina&quot;\n    };\n    answer = 1; \/\/ I don&#039;t know, so this is random...\n    selection = JOptionPane.showOptionDialog(\n        null,\n        &quot;\\nWhich team broke UCLA basketball&#039;s &quot; +\n        &quot;\\n88 game win streak?&quot;,\n        &quot;Question 4 of 5&quot;,\n        JOptionPane.DEFAULT_OPTION,\n        JOptionPane.QUESTION_MESSAGE,\n        null,\n        choices,\n        choices&#x5B;0]\n        );\n    if (selection == answer)\n    {\n      score++;\n    }\n    \n    \/\/ Questions 5 on...\n    \n    int response = JOptionPane.showConfirmDialog(\n        null,\n        &quot;Your score was &quot; + score +\n        &quot;\\n\\nDo you want to play again?&quot;,\n        &quot;Score&quot;,\n        JOptionPane.YES_NO_OPTION);\n    boolean playAgain = false;\n    if (response == JOptionPane.YES_OPTION)\n    {\n      playAgain = true;\n    }\n    return playAgain;\n  }\n}\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which brings up the following dialog:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"323\" height=\"141\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/TriviaGame1.gif\" alt=\"\" class=\"wp-image-1176\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:13px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">and the user can choose to click the appropriate button.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the end of the game, the user gets informed of his\/her score, and gets prompted to play again:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"268\" height=\"141\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/TriviaGameEnd.gif\" alt=\"\" class=\"wp-image-1177\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Getting_More_Sophisticated\"><\/span>Getting More Sophisticated<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">But if there are a number of options, it may be more appropriate to get more sophisticated:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [33,34,35,36,37,38,40,41,42,43,44,47,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.BorderLayout;\nimport java.awt.GridLayout;\n\nimport javax.swing.ButtonGroup;\nimport javax.swing.JOptionPane;\nimport javax.swing.JPanel;\nimport javax.swing.JRadioButton;\nimport javax.swing.JTextArea;\n\npublic class TriviaGame2\n{\n  public static void main(String&#x5B;] args)\n  {\n    boolean playAgain = true;\n    while (playAgain)\n    {\n      playAgain = play();\n    }\n  }\n  \n  private static boolean play()\n  {\n    int score = 0;\n    JRadioButton&#x5B;] choices = null;\n    int answer = -1;\n    int selection = -1;\n    \n    \/\/ Questions 1 through 3...\n    \n    \/\/ Question 4 for sports\n    choices = new JRadioButton&#x5B;]\n    {\n      new JRadioButton(&quot;USC&quot;),\n      new JRadioButton(&quot;Notre Dame&quot;),\n      new JRadioButton(&quot;North Carolina&quot;)\n    };\n    answer = 1; \/\/ I don&#039;t know, so this is random...\n    OptionsPanel optionsPanel =\n        new OptionsPanel(\n        &quot;Which team broke UCLA basketball&#039;s &quot; +\n        &quot;\\n88 game win streak?&quot;,\n        choices);\n    JOptionPane.showOptionDialog(\n        null,\n        optionsPanel,\n        &quot;Question 4 of 5&quot;,\n        JOptionPane.DEFAULT_OPTION,\n        JOptionPane.QUESTION_MESSAGE,\n        null,\n        null,\n        null\n        );\n    selection = optionsPanel.getSelectedOption();\n    if (selection == answer)\n    {\n      score++;\n    }\n    \n    \/\/ Questions 5 on...\n    \n    int response = JOptionPane.showConfirmDialog(\n        null,\n        &quot;Your score was &quot; + score +\n        &quot;\\n\\nDo you want to play again?&quot;,\n        &quot;Score&quot;,\n        JOptionPane.YES_NO_OPTION);\n    boolean playAgain = false;\n    if (response == JOptionPane.YES_OPTION)\n    {\n      playAgain = true;\n    }\n    return playAgain;\n  }\n}\n\nclass OptionsPanel extends JPanel\n{\n  public OptionsPanel(String question, JRadioButton&#x5B;] buttons)\n  {\n    setLayout(new BorderLayout());\n    JTextArea questionArea = new JTextArea(question);\n    questionArea.setEditable(false);\n    questionArea.setBackground(getBackground());\n    add(questionArea, BorderLayout.NORTH);\n    JPanel answersPanel = new JPanel();\n    answersPanel.setLayout(new GridLayout(buttons.length, 1));\n    add(answersPanel, BorderLayout.CENTER);\n    for (int i = 0; i &lt; buttons.length; i++)\n    {\n      if (i == 0)\n      {\n        buttons&#x5B;i].setSelected(true);\n      }\n      answersPanel.add(buttons&#x5B;i]);\n      m_group.add(buttons&#x5B;i]);\n    }\n    m_buttons = buttons;\n  }\n  \n  public int getSelectedOption()\n  {\n    for (int i = 0; i &lt; m_buttons.length; i++)\n    {\n      if (m_buttons&#x5B;i].isSelected())\n      {\n        return i;\n      }\n    }\n    return -1; \/\/ Should never happen\n  }\n  \n  private ButtonGroup m_group = new ButtonGroup();\n  private JRadioButton&#x5B;] m_buttons;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"279\" height=\"193\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/TriviaGame2.gif\" alt=\"\" class=\"wp-image-1178\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:18px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Then there are dialog boxes, of various different types&#8230; JOptionPane Dialogs The&nbsp;JOptionPane&nbsp;class provides a useful set of dialog boxes, from the very simple to the fairly complex.&nbsp; It&#8217;s surprising how much you can accomplish using just one of the static methods provided by&nbsp;JOptionPane.&nbsp; You can create: Message Dialog&nbsp;&#8212; Tells the user about something that has [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":71,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_front_end_style_editor":"no","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-1152","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":"Then there are dialog boxes, of various different types&#8230; JOptionPane Dialogs The&nbsp;JOptionPane&nbsp;class provides a useful set of dialog boxes, from the very simple to the fairly complex.&nbsp; It&#8217;s surprising how much you can accomplish using just one of the static methods provided by&nbsp;JOptionPane.&nbsp; You can create: Message Dialog&nbsp;&#8212; Tells the user about something that has&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1152","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=1152"}],"version-history":[{"count":16,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1152\/revisions"}],"predecessor-version":[{"id":1183,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1152\/revisions\/1183"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/71"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=1152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}