Interface Test

Question: Is it possible for an interfaced service provider to broadcast responses to a number of clients?

Answer: Yes. The code below uses a JButton as service provider. When the button is pressed, a signal is sent to three class A objects. Each object is associated with a JFrame and a JTextField. A message unique to each object is printed in its JTextField.

Here is the code: source                                        Here is the applet:

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.applet.*;

   class A extends JFrame implements ActionListener {
      JTextField text;
      int numb;

      public A (int numb) {
         this.numb = numb;
         add (text = new JTextField());
      }

      public void actionPerformed (ActionEvent evt) {
         text.setText("Class A object hit:"+numb);
      }
   }

   public class InterfaceTest extends Applet {
      JButton button;
      A a,b,c;

      public void init () {
         add (button = new JButton("Press Me"));
         button.addActionListener(a = new A(1));
         button.addActionListener(b = new A(2));
         button.addActionListener(c = new A(3));
         a.setSize(200,50);
         b.setSize(200,50);
         c.setSize(200,50);
         a.setVisible(true);
         b.setVisible(true);
         c.setVisible(true);
      }
   }