home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / ActiveX / Invoke / Example.java next >
Encoding:
Java Source  |  2000-05-04  |  4.0 KB  |  132 lines

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. //  Example.java
  4. //
  5. //      This example demonstrates how to instantiate an AcitveX control
  6. //      using the CLSID instead of using JActiveX tool to generate the
  7. //      JavaBean wrappers for the control. Also, this example demonstrates
  8. //      how to call methods, set properties, and get properties.
  9. //
  10. //      This example relies on the MS Calendar ActiveX control that
  11. //      ships with Microsoft Office 97.
  12. //
  13. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  14. //
  15. //////////////////////////////////////////////////////////////////////////
  16.  
  17. import java.awt.*;
  18. import java.awt.event.*;
  19. import com.ms.activeX.*;
  20. import com.ms.ui.*;
  21. import com.ms.com.*;
  22.  
  23. public class Example extends Frame
  24. {
  25.     private ActiveXControl cal;
  26.  
  27.     /**
  28.      *  The main entry point for the Example application. It will
  29.      *  create a new Example object, set the bounds and make the
  30.      *  app visible.
  31.      *
  32.      *  @param  args[]      Command line argument list.
  33.      *
  34.      *  @return             No return value.
  35.      */
  36.     public static void main(String args[])
  37.     {
  38.         Example app = new Example();
  39.         
  40.         app.setBounds(0, 0, 400, 400);
  41.         app.setVisible(true);
  42.     }
  43.  
  44.     /**
  45.      *  Creates a new Example object. It will add the ActiveX
  46.      *  control, and add buttons to control the application.
  47.      */
  48.     public Example()
  49.     {
  50.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  51.         setLayout(new BorderLayout());
  52.  
  53.         //
  54.         //  create a new panel and add each button to the
  55.         //  panel. the buttons are used to invoke methods,
  56.         //  set properties and get property values
  57.         //
  58.         Panel p = new Panel();
  59.         p.setLayout(new GridLayout(2, 1));
  60.         p.add(new Button("Set the date to the 23rd"));
  61.         p.add(new Button("Get the day"));
  62.         add("West", p);
  63.  
  64.         //
  65.         //  create the ActiveX control using the CLSID
  66.         //
  67.         cal = new ActiveXControl("{8E27C92B-1264-101C-8A2F-040224009C02}");
  68.         add("Center", cal);        
  69.     }
  70.  
  71.     /**
  72.      *  This method is used to look for the WINDOW_CLOSING event.
  73.      *  When the event is caught, exit the application.
  74.      *
  75.      *  @param  event   The event for processing.
  76.      *
  77.      *  @return         No return value.
  78.      */
  79.     public void processWindowEvent(WindowEvent event)
  80.     {
  81.         if (event.getID() == WindowEvent.WINDOW_CLOSING)
  82.             System.exit(0);
  83.  
  84.         super.processWindowEvent(event);        
  85.     }
  86.  
  87.     /**
  88.      *  This method is called when a button is pressed.
  89.      *
  90.      *  @param  event   The event object.
  91.      *  @param  obj     The object.
  92.      *
  93.      *  @return         True is we processed the event,
  94.      *                  otherwise, false.
  95.      */
  96.     public boolean action(Event event, Object obj)
  97.     {
  98.         if (event.target instanceof Button)
  99.         {
  100.             Button b = (Button)event.target;
  101.  
  102.             //
  103.             //  call setProperty to set the Day property
  104.             //  on the ActiveX control.
  105.             //
  106.             if (b.getLabel() == "Set the date to the 23rd")
  107.             {
  108.                 short date = 23;
  109.                 cal.setProperty("Day", date);
  110.             }
  111.             //
  112.             //  this will ask the control for the value of the
  113.             //  day property. the value of this property is the
  114.             //  currently selected day.
  115.             //
  116.             else if (b.getLabel() == "Get the day")
  117.             {
  118.                 Variant month = cal.getProperty("Day");
  119.                 String day = "You have selected " + month.getShort();
  120.  
  121.                 // 
  122.                 //  display the day in an AFC message box.
  123.                 //
  124.                 UIMessageBox mbox = new UIMessageBox(new UIFrame(), "The Day Property", day, UIMessageBox.STOP, UIButtonBar.OK);
  125.                 mbox.doModal();
  126.             }
  127.         }
  128.  
  129.         return false;
  130.     }
  131. }
  132.