{"id":846,"date":"2021-01-11T19:53:06","date_gmt":"2021-01-11T19:53:06","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=846"},"modified":"2021-01-11T20:10:52","modified_gmt":"2021-01-11T20:10:52","slug":"event-multicasting","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/events\/event-multicasting\/","title":{"rendered":"Event Multicasting"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Given Java&#8217;s <em>producer\/consumer delegation model<\/em>, it is possible for:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>many <em>consumers<\/em> (listeners) to listen to a single <em>producer<\/em><\/li><li>a single <em>consumer<\/em> to listen to many <em>producers<\/em><\/li><li>both<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of a simple application that illustrates this.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The <strong>Create Frame<\/strong> button has a single listener &#8212;&nbsp;<code><strong>MulticastPanel<\/strong><\/code><\/li><li>The <strong>Close All<\/strong> button has multiple listeners &#8212; every&nbsp;<code><strong>SimpleFrame<\/strong><\/code><\/li><li>The <strong>WindowListener<\/strong>,&nbsp;<code><strong>m_windowListener<\/strong><\/code>, an instance member of&nbsp;<code><strong>MulticastPanel<\/strong><\/code>, is a single listener that listens for window closing events from every&nbsp;<code><strong>SimpleFrame<\/strong><\/code>.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [18,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,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]; title: ; quick-code: false; notranslate\" title=\"\">\npackage swingExamples;\n\nimport java.awt.Container;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.awt.event.WindowAdapter;\nimport java.awt.event.WindowEvent;\nimport java.awt.event.WindowListener;\n\nimport javax.swing.JButton;\nimport javax.swing.JFrame;\nimport javax.swing.JPanel;\n\n\/**\n * The main panel for the Multicaster main frame\n *\/\nclass MulticastPanel extends JPanel\n    implements ActionListener\n{\n  public MulticastPanel()\n  {\n    m_newButton = new JButton(&quot;Create Frame&quot;);\n    m_newButton.addActionListener(this);\n    add(m_newButton);\n    \n    m_closeAllButton = new JButton(&quot;Close All&quot;);\n    m_closeAllButton.setEnabled(false);\n    add(m_closeAllButton);\n    \n    \/\/ Listens for SimpleFrames to close\n    m_windowListener = new WindowAdapter()\n    {\n      public void windowClosing(WindowEvent ev)\n      {\n        \/\/ One fewer SimpleFrame visible\n        if (--m_totalFrameCount == 0)\n        {\n          \/\/ Once there are no more SimpleFrames left,\n          \/\/ disable the Close All button.\n          m_closeAllButton.setEnabled(false);\n        }\n      }\n    };\n  }\n  \n  public void actionPerformed(ActionEvent event)\n  {\n    Object source = event.getSource();\n    if (source == m_newButton)\n    {\n      \/\/ Create Frame button click causes \n      \/\/ a SimpleFrame to be created\n      SimpleFrame frame = new SimpleFrame(++m_nextFrameNo);\n      \/\/ Keeps track of how many SimpleFrames \n      \/\/ are currently up.\n      m_totalFrameCount++;\n      \/\/ SimpleFrame listens for Close All button clicks.\n      m_closeAllButton.addActionListener(frame);\n      \/\/ At least one SimpleFrame up, so enable the \n      \/\/ Close All button.\n      m_closeAllButton.setEnabled(true);\n      \/\/ Listens for SimpleFrame to close\n      frame.addWindowListener(m_windowListener);\n      \/\/ Makes SimpleFrame visible\n      frame.setVisible(true);\n    }\n  }\n  \n  \/\/\/\/\/\/\/\/\/\/\/ Private data \/\/\/\/\/\/\/\/\/\/\n  private JButton m_newButton;\n  private JButton m_closeAllButton;\n  private WindowListener m_windowListener;\n  \n  private static int m_nextFrameNo = 0; \n          \/\/ Frame number for next frame\n  private static int m_totalFrameCount = 0; \n          \/\/ Count of frames currently visible\n}\n\n\/**\n * Frame to be displayed when user clicks on \n * &quot;Create Frame&quot; button.\n *\/\nclass SimpleFrame extends JFrame\n    implements ActionListener\n{\n  SimpleFrame(int frameNumber)\n  {\n    setSize(300, 200);\n    \/\/ Calculate an offset to locate the SimpleFrame \n    \/\/ on the screen\n    int offset = 30 * ( (frameNumber - 1) % 10);\n    setLocation(offset, offset);\n    \/\/ Give it a title\n    setTitle(&quot;Frame &quot; + frameNumber);\n  }\n  \n  public void actionPerformed(ActionEvent ev)\n  {\n    \/\/ This method is invoked by a Close All button click\n    \n    \/\/ Make the frame invisible\n    setVisible(false);\n    \/\/ The above doesn&#039;t generate an event, so create one \n    \/\/ and dispatch it so that the window listener is informed\n    dispatchEvent(new WindowEvent(\n        this, \n        WindowEvent.WINDOW_CLOSING));\n    \/\/ Get rid of this frame&#039;s resources\n    dispose();\n  }\n}\n\n\/**\n * The &quot;main&quot; frame class\n *\/\nclass MulticastFrame extends JFrame\n{\n  public MulticastFrame()\n  {\n    setTitle(&quot;MulticastTest&quot;);\n    setSize(100, 50);\n    setDefaultCloseOperation(EXIT_ON_CLOSE);\n    Container contentPane = getContentPane();\n    contentPane.add(new MulticastPanel());\n    pack(); \/\/ Size, based on contents of frame\n  }\n}\n\npublic class Multicaster\n{\n  public static void main(String&#x5B;] args)\n  {\n    MulticastFrame frame = new MulticastFrame();\n    frame.setVisible(true);\n  }\n}\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">When the application starts, there are no&nbsp;<code><strong>SimpleFrame<\/strong><\/code>s, and so the <strong>Close All <\/strong>button is disabled:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img decoding=\"async\" width=\"217\" height=\"70\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/MulticastTest1.gif\" alt=\"\" class=\"wp-image-850\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:31px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">When the <strong>Create Frame<\/strong> button is clicked, a&nbsp;<code><strong>SimpleFrame<\/strong><\/code>&nbsp;is created and made visible, and the <strong>Close All<\/strong> button is enabled.&nbsp; This continues as long as there is at least one&nbsp;<code><strong>SimpleFrame<\/strong><\/code>&nbsp;visible.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the last&nbsp;<code><strong>SimpleFrame<\/strong><\/code>&nbsp;is closed, the <strong>Close All<\/strong> button is once again disabled.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s what the application looks like with a set of simple frames visible:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"alignleft size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"651\" height=\"450\" src=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-content\/uploads\/2021\/01\/MulticastTest2.gif\" alt=\"\" class=\"wp-image-851\"\/><\/figure><\/div>\n\n\n\n<div style=\"height:27px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given Java&#8217;s producer\/consumer delegation model, it is possible for: many consumers (listeners) to listen to a single producer a single consumer to listen to many producers both Here&#8217;s an example of a simple application that illustrates this.&nbsp;&nbsp; Note the following: The Create Frame button has a single listener &#8212;&nbsp;MulticastPanel The Close All button has multiple [&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-846","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":"Given Java&#8217;s producer\/consumer delegation model, it is possible for: many consumers (listeners) to listen to a single producer a single consumer to listen to many producers both Here&#8217;s an example of a simple application that illustrates this.&nbsp;&nbsp; Note the following: The Create Frame button has a single listener &#8212;&nbsp;MulticastPanel The Close All button has multiple&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/846","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=846"}],"version-history":[{"count":6,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/846\/revisions"}],"predecessor-version":[{"id":856,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/846\/revisions\/856"}],"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=846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}