20-CS-694 Advanced Programming Techniques Spring 2012
Widgets

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous      Next      Applet Source      All lectures     

Dialog Box Test

An "application" box, identified as having a JButton labeled "Press Here," appears immediately. In three seconds a <yes|no|maybe> query box and another "application" box appear. Choose an option in the query box. The option value is shown in both application boxes. Three seconds later a query box appears with another "application" box. That option is sent to and displayed in the most recent and earliest "application" boxes. Three seconds later another query box and "application" box appear. The process repeats indefinitely.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

class QueryBox extends Dialog implements ActionListener {
   private String result;
   Button yes, may, not;
   
   QueryBox (Frame f, String question) {
      super (f, question);
      add ("Center", new Label(question, Label.CENTER));
      Panel p = new Panel();
      p.add (yes = new Button("Yes"));
      p.add (may = new Button("Maybe"));
      p.add (not = new Button("No"));
      yes.addActionListener(this);
      may.addActionListener(this);
      not.addActionListener(this);
      add("South", p);
      setSize(200,100);
      setVisible(true);
   }
   
   synchronized public String answer () {
      try {
         wait();
      }
      catch (Exception e) { }
      return result;
   }
   
   synchronized public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == yes) result = "Yes";
      else if (evt.getSource() == may) result = "Maybe";
      else if (evt.getSource() == not) result = "No";
      notifyAll();
      dispose();
   }
}

class Application extends Frame implements ActionListener {
   Button b;
   JTextField t, out;
   int i=0;
   
   Application() {
      setLayout(new BorderLayout());
      add ("North", out = new JTextField());
      Panel p = new Panel();
      p.setLayout(new GridLayout(1,2));
      p.add(b = new Button("Press Here"));
      p.add(t = new JTextField());
      b.addActionListener(this);
      add("Center", p);
      out.setEditable(false);
      setSize(200,100);
      setVisible(true);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == b) t.setText(String.valueOf(i++));
   }
}

public class Query extends Applet implements Runnable, ActionListener {
   String ans;
   Button go;
   JTextField text, out;
   Thread runner;
   int i=0;
   
   public void init () {
      setLayout(new BorderLayout());
      add("North", text = new JTextField());
      Panel p = new Panel();
      p.setLayout(new GridLayout(1,2));
      p.add(go = new Button("Press Here"));
      p.add(out = new JTextField());
      go.addActionListener(this);
      add ("Center", p);
      text.setEditable(false);
      setSize(150,70);
      
      runner = new Thread(this);
      runner.start();
   }

   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == go) out.setText(String.valueOf(i++));
   }
   
   public void run () {
      while (true) {
         try {
            runner.sleep(3000);
         }
         catch (Exception e) { }

         Application app = new Application();
         QueryBox query = new QueryBox (app, "Hello There");
         text.setText(ans=query.answer());
         app.out.setText(ans);
      }
   }
}