{"id":822,"date":"2021-01-11T19:31:36","date_gmt":"2021-01-11T19:31:36","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=822"},"modified":"2021-01-11T19:44:23","modified_gmt":"2021-01-11T19:44:23","slug":"menus","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/menus\/","title":{"rendered":"Menus"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 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-6a06e3dcb5f7c\" 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-6a06e3dcb5f7c\"  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\/events\/menus\/#A_Simple_Menu_Example\" >A Simple Menu Example<\/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\/events\/menus\/#A_More_Complex_Menu_Example\" >A More Complex Menu Example<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>We would, of course, like to have menus in our frames.<\/p>\n\n\n\n<p>Here&#8217;s how to do it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Simple_Menu_Example\"><\/span>A Simple Menu Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s a simple menu example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport javax.swing.JFrame;\nimport javax.swing.JMenu;\nimport javax.swing.JMenuBar;\n\npublic class SimpleMenu extends JFrame\n{\n  public SimpleMenu(String title)\n  {\n    setTitle(title);\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    JMenuBar menuBar = new JMenuBar();\n    menuBar.add(fileMenu);\n    setJMenuBar(menuBar);\n  }\n  \n  public static void main(String&#x5B;] args)\n  {\n    SimpleMenu frame = new SimpleMenu(&quot;SimpleMenu&quot;);\n    frame.setSize(300, 200);\n    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);\n    frame.setVisible(true);\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces:<\/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\/SimpleMenu.gif\" alt=\"\" class=\"wp-image-826\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p>Of course, you can click on the menu items all you want, but they won&#8217;t do anything for you.&nbsp; <\/p>\n\n\n\n<p>For that you need events&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_More_Complex_Menu_Example\"><\/span>A More Complex Menu Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you want to add other menus (including a popup menu), and actually hook up some of the actions, here&#8217;s how you might do it.<\/p>\n\n\n\n<p>Here, we make some more complex menu structures, including a pop-up menu.&nbsp; We show a simple example of how to hook up a menu item (the&nbsp;<strong>Help -&gt; About MenuTest&#8230;<\/strong>&nbsp;item) so that it will respond appropriately.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [9,10,12,13,14,15,16,17,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,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,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.awt.event.MouseAdapter;\nimport java.awt.event.MouseEvent;\nimport java.awt.event.MouseListener;\n\nimport javax.swing.ButtonGroup;\nimport javax.swing.JComponent;\nimport javax.swing.JFrame;\nimport javax.swing.JMenu;\nimport javax.swing.JMenuBar;\nimport javax.swing.JMenuItem;\nimport javax.swing.JOptionPane;\nimport javax.swing.JPopupMenu;\nimport javax.swing.JRadioButtonMenuItem;\n\npublic class ComplexMenu extends JFrame \n    implements ActionListener\n{\n  public ComplexMenu(String title)\n  {\n    super(title);\n\n    \/\/ Create menu bar with menus\n    JMenuBar menuBar = new JMenuBar();\n    setJMenuBar(menuBar);\n\n    \/\/ File menu\n    JMenu fileMenu = new JMenu(&quot;File&quot;);\n    menuBar.add(fileMenu);\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    m_quit = new JMenuItem(&quot;Quit&quot;);\n    m_quit.addActionListener(this);\n    fileMenu.add(m_quit);\n\n    \/\/ Edit menu\n    JMenu editMenu = new JMenu(&quot;Edit&quot;);\n    menuBar.add(editMenu);\n    editMenu.add(&quot;Cut&quot;);\n    editMenu.add(&quot;Copy&quot;);\n    editMenu.add(&quot;Paste&quot;);\n\n    \/\/ Shape menu\n    JMenu shapeMenu = new JMenu(&quot;Shape&quot;);\n    menuBar.add(shapeMenu);\n    shapeMenu.add(getShapesSubMenu());\n    shapeMenu.add(&quot;Properties...&quot;);\n\n    \/\/ Help menu\n    JMenu helpMenu = new JMenu(&quot;Help&quot;);\n    menuBar.add(helpMenu);\n    helpMenu.add(&quot;Help Overview...&quot;);\n    helpMenu.add(&quot;Help Topics...&quot;);\n    m_about = new JMenuItem(&quot;About MenuTest...&quot;);\n    m_about.addActionListener(this);\n    helpMenu.add(m_about);\n\n    \/\/ Create the popup menu.\n    JPopupMenu popupMenu = new JPopupMenu();\n    addShapes(popupMenu);\n    \/\/ Add listener to components that \n    \/\/ can bring up popup menus.\n    MouseListener popupListener = \n         new PopupListener(popupMenu);\n    addMouseListener(popupListener);\n  }\n\n  private JMenu getShapesSubMenu()\n  {\n    JMenu shapesSubMenu = new JMenu(&quot;Shapes&quot;);\n    addShapes(shapesSubMenu);\n\n    return shapesSubMenu;\n  }\n\n  private void addShapes(JComponent menu)\n  {\n    ButtonGroup group = new ButtonGroup();\n\n    JRadioButtonMenuItem square = \n          new JRadioButtonMenuItem(&quot;Square&quot;, true);\n    group.add(square);\n    menu.add(square);\n    JRadioButtonMenuItem circle = \n          new JRadioButtonMenuItem(&quot;Circle&quot;);\n    group.add(circle);\n    menu.add(circle);\n    JRadioButtonMenuItem triangle = \n          new JRadioButtonMenuItem(&quot;Triangle&quot;);\n    group.add(triangle);\n    menu.add(triangle);\n    menu.add(getPropertiesSubMenu());\n  }\n\n  private JMenu getPropertiesSubMenu()\n  {\n    ButtonGroup group = new ButtonGroup();\n    JMenu propertiesSubMenu = new JMenu(&quot;Properties&quot;);\n\n    JRadioButtonMenuItem solid = \n          new JRadioButtonMenuItem(&quot;Solid&quot;, true);\n    group.add(solid);\n    propertiesSubMenu.add(solid);\n    JRadioButtonMenuItem outline = \n          new JRadioButtonMenuItem(&quot;Outline&quot;);\n    group.add(outline);\n    propertiesSubMenu.add(outline);\n\n    return propertiesSubMenu;\n  }\n\n  public void actionPerformed(ActionEvent event)\n  {\n    Object obj = event.getSource();\n    if (obj == m_quit)\n    {\n      System.exit(0);\n    }\n    else if (obj == m_about)\n    {\n      showAboutBox();\n    }\n  }\n\n  private void showAboutBox()\n  {\n    JOptionPane.showMessageDialog(\n        this,\n        &quot;  MenuTest 1.0\\n&quot; +\n        &quot;       by\\n&quot; +\n        &quot;   Bryan J. Higgs\\n&quot; +\n        &quot;  October 31, 2000\\n&quot; +\n        &quot;  HAPPY HALLOWEEN!&quot;,\n        &quot;About MenuTest&quot;,\n        JOptionPane.INFORMATION_MESSAGE);\n  }\n\n  public static void main(String&#x5B;] args)\n  {\n    ComplexMenu frame = new ComplexMenu(&quot;MenuTest&quot;);\n    frame.setSize(300, 200);\n    frame.setVisible(true);\n  }\n\n  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Inner classes \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n  class PopupListener\n      extends MouseAdapter\n  {\n    PopupListener(JPopupMenu menu)\n    {\n      m_popup = menu;\n    }\n\n    public void mousePressed(MouseEvent e)\n    {\n      maybeShowPopup(e);\n    }\n\n    public void mouseReleased(MouseEvent e)\n    {\n      maybeShowPopup(e);\n    }\n\n    private void maybeShowPopup(MouseEvent e)\n    {\n      if (e.isPopupTrigger())\n      {\n        m_popup.show(e.getComponent(),\n                     e.getX(), e.getY());\n      }\n    }\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/ Private Data \/\/\/\/\/\/\/\/\/\/\/\/\/\n    private JPopupMenu m_popup;\n  }\n\n  \/\/\/\/\/\/\/\/\/ Private Data \/\/\/\/\/\/\/\n  private JMenuItem m_quit;\n  private JMenuItem m_about;\n}\n<\/pre><\/div>\n\n\n<p>Here&#8217;s what it produces:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu1.gif\" alt=\"\" class=\"wp-image-827\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"300\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu2.gif\" alt=\"\" class=\"wp-image-828\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu3.gif\" alt=\"\" class=\"wp-image-829\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu4.gif\" alt=\"\" class=\"wp-image-830\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu5.gif\" alt=\"\" class=\"wp-image-831\"\/><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"360\" height=\"200\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/ComplexMenu6.gif\" alt=\"\" class=\"wp-image-832\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:34px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We would, of course, like to have menus in our frames. Here&#8217;s how to do it. A Simple Menu Example Here&#8217;s a simple menu example: which produces: Of course, you can click on the menu items all you want, but they won&#8217;t do anything for you.&nbsp; For that you need events&#8230; A More Complex Menu [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":69,"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-822","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":"We would, of course, like to have menus in our frames. Here&#8217;s how to do it. A Simple Menu Example Here&#8217;s a simple menu example: which produces: Of course, you can click on the menu items all you want, but they won&#8217;t do anything for you.&nbsp; For that you need events&#8230; A More Complex Menu&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/822","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=822"}],"version-history":[{"count":5,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/822\/revisions"}],"predecessor-version":[{"id":836,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/822\/revisions\/836"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/69"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}