Multicasting

   


In the preceding section, we had several event sources report to the same event listener. In this section, we do the opposite. All AWT event sources support a multicast model for listeners. This means that the same event can be sent to more than one listener object. Multicasting is useful if an event is potentially of interest to many parties. Simply add multiple listeners to an event source to give all registered listeners a chance to react to the events.

CAUTION

According to the JDK documentation, "The API makes no guarantees about the order in which the events are delivered to a set of registered listeners for a given event on a given source." Therefore, don't implement program logic that depends on the delivery order.


Here we show a simple application of multicasting. We will have a frame that can spawn multiple windows with the New button, and it can close all windows with the Close all button see Figure 8-10.

Figure 8-10. All frames listen to the Close all command


The listener to the New button of the MulticastPanel is the newListener object constructed in the MulticastPanel constructor it makes a new frame whenever the button is clicked.

But the Close all button of the MulticastPanel has multiple listeners. Each time the BlankFrame constructor executes, it adds another action listener to the Close all button. Each of those listeners is responsible for closing a single frame in its actionPerformed method. When the user clicks the Close all button, each of the listeners is activated and each of them closes its frame.

Furthermore, the actionPerformed method removes the listener from the Close all button because it is no longer needed once the frame is closed.

Example 8-6 shows the source code.

Example 8-6. MulticastTest.java
  1. import java.awt.*;  2. import java.awt.event.*;  3. import javax.swing.*;  4.  5. public class MulticastTest  6. {  7.    public static void main(String[] args)  8.    {  9.       MulticastFrame frame = new MulticastFrame(); 10.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11.       frame.setVisible(true); 12.    } 13. } 14. 15. /** 16.    A frame with buttons to make and close secondary frames 17. */ 18. class MulticastFrame extends JFrame 19. { 20.    public MulticastFrame() 21.    { 22.       setTitle("MulticastTest"); 23.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 24. 25.       // add panel to frame 26. 27.       MulticastPanel panel = new MulticastPanel(); 28.       add(panel); 29.    } 30. 31.    public static final int DEFAULT_WIDTH = 300; 32.    public static final int DEFAULT_HEIGHT = 200; 33. } 34. 35. /** 36.    A panel with buttons to create and close sample frames. 37. */ 38. class MulticastPanel extends JPanel 39. { 40.    public MulticastPanel() 41.    { 42.       // add "New" button 43. 44.       JButton newButton = new JButton("New"); 45.       add(newButton); 46.       final JButton closeAllButton = new JButton("Close all"); 47.       add(closeAllButton); 48. 49.       ActionListener newListener = new 50.          ActionListener() 51.          { 52.             public void actionPerformed(ActionEvent event) 53.             { 54.                BlankFrame frame = new BlankFrame(closeAllButton); 55.                frame.setVisible(true); 56.             } 57.          }; 58. 59.       newButton.addActionListener(newListener); 60.    } 61. } 62. 63. /** 64.    A blank frame that can be closed by clicking a button. 65. */ 66. class BlankFrame extends JFrame 67. { 68.    /** 69.       Constructs a blank frame 70.       @param closeButton the button to close this frame 71.    */ 72.    public BlankFrame(final JButton closeButton) 73.    { 74.       counter++; 75.       setTitle("Frame " + counter); 76.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); 77.       setLocation(SPACING * counter, SPACING * counter); 78. 79.       closeListener = new 80.          ActionListener() 81.          { 82.             public void actionPerformed(ActionEvent event) 83.             { 84.                closeButton.removeActionListener(closeListener); 85.                dispose(); 86.             } 87.          }; 88.       closeButton.addActionListener(closeListener); 89.    } 90. 91.    private ActionListener closeListener; 92.    public static final int DEFAULT_WIDTH = 200; 93.    public static final int DEFAULT_HEIGHT = 150; 94.    public static final int SPACING = 40; 95.    private static int counter = 0; 96. } 


       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net