home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP17 / DIALOGWINDOW / DIALOGWINDOW.JAVA < prev    next >
Encoding:
Java Source  |  1996-06-13  |  3.7 KB  |  156 lines

  1. // DialogWindow - pop up the dialog box created by the
  2. //                ResourceWizard and wait for the user to enter
  3. //                OK. once he or she does, read the data out of
  4. //                the dialog box, close the dialog box, and
  5. //                display the data in the applet window
  6. import java.applet.*;
  7. import java.awt.*;
  8. import AutoDialog;
  9.  
  10. public class DialogWindow extends Applet
  11. {
  12.  
  13.     // the extra frame attached to the applet
  14.     private OurFrame m_frame = null;
  15.  
  16.     public DialogWindow()
  17.     {
  18.         // TODO: Add Constructor code here
  19.     }
  20.  
  21.     public String getAppletInfo()
  22.     {
  23.         return "Name: DialogWindow\r\n" +
  24.                "Author: Stephan R. Davis\r\n" +
  25.                "Created for Learn Java Now";
  26.     }
  27.  
  28.  
  29.     public void init()
  30.     {
  31.         resize(320, 240);
  32.  
  33.         // TODO: Place Addition Initialization code here
  34.         // Create one of our dialog box frames
  35.         m_frame = new OurFrame(this);
  36.     }
  37.  
  38.     public void destroy()
  39.     {
  40.         // TODO: Place applet cleanup code here
  41.     }
  42.  
  43.     public void paint(Graphics g)
  44.     {
  45.         DialogData data = m_frame.getData();
  46.         if (data != null)
  47.         {
  48.             g.drawString(data.toString(), 10, 20);
  49.         }
  50.     }
  51.  
  52.     public void start()
  53.     {
  54.         // TODO: Place additional applet Start code here
  55.     }
  56.     
  57.     public void stop()
  58.     {
  59.     }
  60.  
  61.  
  62.  
  63.  
  64.     // TODO: Place additional applet Code here
  65.  
  66. }
  67.  
  68. class OurFrame extends Frame
  69. {
  70.     // child dialog box
  71.     private AutoDialog m_autodialog = null;
  72.  
  73.     // parent applet
  74.     private Applet m_parent = null;
  75.  
  76.     // data contained in dialog box
  77.     private DialogData m_data = null;
  78.  
  79.     public OurFrame(Applet parent)
  80.     {
  81.         super("Auto Dialog");
  82.  
  83.         m_parent = parent;
  84.  
  85.         // be sure to set a font (the size is unimportant
  86.         // because it will be changed by the AutoDialog class)
  87.         setFont(new Font("Arial", Font.PLAIN, 12));
  88.  
  89.         // now create the AutoDialog and let it add the
  90.         // components to our frame
  91.         m_autodialog = new AutoDialog(this);
  92.         m_autodialog.CreateControls();
  93.  
  94.         show();
  95.     }
  96.  
  97.     public boolean action(Event event, Object obj)
  98.     {
  99.       // if this is our button...
  100.       Object target = event.target;
  101.       if (target instanceof Button)
  102.       {
  103.           Button button = (Button)target;
  104.           String buttonLabel = button.getLabel();
  105.           if (buttonLabel.compareTo("OK") == 0)
  106.           {
  107.               // ...get the data from the dialog box...
  108.               String sFirstName=m_autodialog.IDC_EDIT1.getText();
  109.               String sLastName =m_autodialog.IDC_EDIT2.getText();
  110.               boolean bMarried =
  111.                               m_autodialog.IDC_CHECK1.getState();
  112.  
  113.               // ...create a DialogData object...
  114.               m_data = new DialogData(sFirstName,
  115.                                       sLastName,
  116.                                       bMarried);
  117.  
  118.               // ...and force the applet to output the data
  119.               m_parent.repaint();
  120.           }
  121.  
  122.           // hide the dialog box if either button is pressed
  123.           hide();
  124.           return true;
  125.       }
  126.       return false;
  127.     }
  128.  
  129.     public DialogData getData()
  130.     {
  131.         return m_data;
  132.     }
  133. }
  134.  
  135. class DialogData
  136. {
  137.     private String m_sFirstName;
  138.     private String m_sLastName;
  139.     private boolean m_bMarried;
  140.  
  141.     public DialogData(String sFN, String sLN, boolean bM)
  142.     {
  143.         m_sFirstName = sFN;
  144.         m_sLastName  = sLN;
  145.         m_bMarried   = bM;
  146.     }
  147.  
  148.     public String toString()
  149.     {
  150.         String s;
  151.         s  = m_sFirstName + " " + m_sLastName + " ";
  152.         s += m_bMarried ? "(married)" : "(single)";
  153.         return s;
  154.     }
  155. }
  156.