home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / MsDev / Samples / Microsoft / HTMLControls / updatecontrols.java < prev   
Encoding:
Java Source  |  1996-08-27  |  5.6 KB  |  205 lines

  1. //******************************************************************************
  2. // updatecontrols.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import intrinsc.*;
  8.     // *** Must run Java Type Library Wizard from    ***
  9.     // *** the tools menu to make import successful. ***
  10.  
  11. //==============================================================================
  12. // Main Class for applet updatecontrols
  13. //
  14. //==============================================================================
  15. public class updatecontrols extends Applet
  16. {
  17.  
  18.     // The HTML controls on the web page
  19.     ICombo javaList;
  20.     IButton javaButton;
  21.     ICheckbox javaCheckbox;
  22.     IText javaText;
  23.  
  24.     // The field and button for updating the <select> control
  25.     TextField newLstIndex = new TextField("4");
  26.     java.awt.Button updateLst = new java.awt.Button("Select Option");
  27.     
  28.     // The field and button for updating the <input type=button> control
  29.     String value1 = "New Name";
  30.     String value2 = "Other Name";
  31.     TextField newBtnValue = new TextField(value1);
  32.     java.awt.Button updateBtn = new java.awt.Button("Change Button Caption");
  33.  
  34.     // The field and button for updating the <input type=text> control
  35.     String value3 = "New Input";
  36.     String value4 = "Other Input";
  37.     TextField newTxtValue = new TextField(value3);
  38.     java.awt.Button updateTxt = new java.awt.Button("Change Input Text");
  39.  
  40.     // The button for updating the <input type=checkbox> control
  41.     java.awt.Button updateChk = new java.awt.Button("Change Checkbox");
  42.     
  43.     // updatecontrols Class Constructor
  44.     //--------------------------------------------------------------------------
  45.     public updatecontrols()
  46.     {
  47.     }
  48.  
  49.     // APPLET INFO SUPPORT:
  50.     //--------------------------------------------------------------------------
  51.     public String getAppletInfo()
  52.     {
  53.         return "Name: updatecontrols\r\n" +
  54.                "Created with Microsoft Visual J++ Version 1.0";
  55.     }
  56.  
  57.  
  58.     // init()
  59.     //--------------------------------------------------------------------------
  60.     public void init()
  61.     {
  62.         resize(270, 130);
  63.  
  64.         // set up the form for output
  65.         setLayout(new GridLayout(4,2,10,10));
  66.  
  67.         add(newLstIndex);
  68.         add(updateLst);
  69.  
  70.         add(newBtnValue);
  71.         add(updateBtn);
  72.  
  73.         add(newTxtValue);
  74.         add(updateTxt);
  75.  
  76.         add(updateChk);
  77.     }
  78.  
  79.     // destroy()
  80.     //-------------------------------------------------------------------------
  81.     public void destroy()
  82.     {
  83.     }
  84.  
  85.     // updatecontrols Paint Handler
  86.     //--------------------------------------------------------------------------
  87.     public void paint(Graphics g)
  88.     {
  89.     }
  90.  
  91.     // start().
  92.     //--------------------------------------------------------------------------
  93.     public void start()
  94.     {
  95.     }
  96.     
  97.     // stop()
  98.     //--------------------------------------------------------------------------
  99.     public void stop()
  100.     {
  101.     }
  102.     
  103.     // loadcontrols()
  104.     // The loadcontrols() function is called from the VBScript code after the page
  105.     // has completely loaded.  VBScript passes in all of the controls so the Java
  106.     // class can modify them.
  107.     //--------------------------------------------------------------------------
  108.     public void loadControls(Object htmlList, 
  109.                              Object htmlButton, 
  110.                              Object htmlText,
  111.                              Object htmlCheckbox)  {
  112.         // storing access to the HTML controls
  113.  
  114.         // javaVar     =    Cast        htmlVar
  115.         javaButton   = (IButton)   htmlButton;
  116.         javaList     = (ICombo)    htmlList;
  117.         javaText     = (IText)     htmlText;
  118.         javaCheckbox = (ICheckbox) htmlCheckbox;
  119.     }
  120.     
  121.     // action()
  122.     // The following code will monitor for a click on any of the
  123.     // java.awt.Button controls then each case will dispatch a call
  124.     // using COM to the controls on the HTML page.
  125.     //--------------------------------------------------------------------------
  126.     public boolean action(Event evt, Object what) {
  127.  
  128.         // Update the <select> control
  129.         if (evt.target == updateLst) {
  130.             // Get the value in the TextField and convert it to an int
  131.             int temp = Integer.valueOf(newLstIndex.getText()).intValue();
  132.  
  133.             // Insure that the value is within range
  134.             if(temp < 0) temp = 0;
  135.             int length = javaList.getlength();
  136.             if(temp > length - 1) temp = length - 1;
  137.  
  138.             // Change the selection in the control
  139.             javaList.putselectedIndex(temp);
  140.  
  141.             // Update the value in the TextField
  142.             temp = (temp + 1) % length;
  143.             newLstIndex.setText(Integer.toString(temp));
  144.  
  145.             // Event handled
  146.             return true;
  147.         }
  148.  
  149.         // Update the <input type=button> control
  150.         if (evt.target == updateBtn) {
  151.             
  152.             // Get the text from the TextField
  153.             String str = newBtnValue.getText();
  154.  
  155.             // Update the caption (value) on the button
  156.             javaButton.putvalue(str);
  157.  
  158.             // Change the value in the TextField
  159.             if(str.compareTo(value1) == 0)
  160.                 newBtnValue.setText(value2);
  161.             else
  162.                 newBtnValue.setText(value1);
  163.  
  164.             // Event handled
  165.             return true;
  166.         }
  167.  
  168.         // Update the <input type=text> control
  169.         if (evt.target == updateTxt) {
  170.             
  171.             // Get the text from the TextField
  172.             String str = newTxtValue.getText();
  173.  
  174.             // Update the <input type=text> control
  175.             javaText.putvalue(str);
  176.  
  177.             // Change the value in the TextField
  178.             if(str.compareTo(value3) == 0)
  179.                 newTxtValue.setText(value4);
  180.             else
  181.                 newTxtValue.setText(value3);
  182.  
  183.             // Event handled
  184.             return true;
  185.         }
  186.  
  187.         // Update the <input type=checkbox> control
  188.         if (evt.target == updateChk) {
  189.  
  190.             // Toggle the state of the control
  191.             if (javaCheckbox.getchecked()) 
  192.                 javaCheckbox.putchecked(false);
  193.             else
  194.                 javaCheckbox.putchecked(true);
  195.             
  196.             // Event handled
  197.             return true;
  198.         }
  199.         
  200.         // Event not handled
  201.         return false;
  202.     }
  203.     
  204. }
  205.