home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Micrsoft / SAMPLES / VJ6SAMPL.EXE / BindPubs / InputBox.java < prev    next >
Encoding:
Java Source  |  1998-02-24  |  4.3 KB  |  151 lines

  1.  
  2.  
  3. /*  Copyright (C) Microsoft Corporation, 1996-1998.  All rights reserved.
  4.  
  5.   This source code is intended only as a supplement to Microsoft
  6.   Visual J++ 6.0.  See this product's on-line documentation for detailed   
  7. information regarding Microsoft code samples.
  8.  
  9. */
  10.  
  11. import wfc.app.*;
  12. import wfc.core.*;
  13. import wfc.ui.*;
  14.  
  15. /**To use an InputBox:
  16.  * 1. create one using a constructor
  17.  *    a. When using the default constructor, use setText(String title) to set
  18.  * the window text; use setResponse(String response) to set the default Response; 
  19.  * use setPrompt(String promptText) to set the prompt
  20.  * 2. show the InputBox as a dialog
  21.  *    a. e.g.    InputBox warmGun = new InputBox();
  22.  *            warmGun.showDialog();//see next point -- the integer this returns is useful
  23.  * 3. take the text from the edit box using getResponse()
  24.  *    a. e.g.    if(warmGun.showDialog()==DialogResult.OK)
  25.  *                happiness = warmGun.getResponse();
  26.  *            else
  27.  *                throw new IllegalArgumentException("One must hit \'OK\' after entering a value")
  28.  */
  29. public class InputBox extends Form
  30. {
  31.     /**Default Constructor -- useable
  32.      */
  33.     public InputBox()
  34.     {
  35.         initForm();        
  36.     }
  37.     /**Make an InputBox with a custom prompt and no default response
  38.      */
  39.     public InputBox(String promptText)
  40.     {
  41.         this();
  42.         setPrompt(promptText);
  43.         setResponse(null);
  44.     }
  45.     /**Make an InputBox with a custom prompt, title, and default response
  46.      */
  47.     public InputBox(String promptText, String title, String defaultResponse)
  48.     {
  49.         this();
  50.         prompt.setText(promptText);
  51.         setText(title);
  52.         setResponse(defaultResponse);
  53.     }
  54.  
  55.  
  56.     /**
  57.     * NOTE: The following code is required by the Visual J++ form
  58.     * designer.  It can be modified using the form editor.  Do not
  59.     * modify it using the code editor.
  60.     */
  61.     Container components = new Container();
  62.     Label prompt = new Label();
  63.     Edit respond = new Edit();
  64.     Button ok = new Button();
  65.  
  66.     private void initForm()
  67.     {
  68.  
  69.         this.setAnchor(ControlAnchor.BOTTOM);
  70.         this.setBackColor(Color.CONTROL);
  71.         this.setLocation(new Point(0, 0));
  72.         this.setSize(new Point(416, 154));
  73.         this.setTabIndex(-1);
  74.         this.setTabStop(true);
  75.         this.setText("Visual J++ 6.0 Sample Code");
  76.         this.setAutoScaleBaseSize(16);
  77.         this.setClientSize(new Point(408, 127));
  78.         prompt.setLocation(new Point(30, 10));
  79.         prompt.setSize(new Point(350, 30));
  80.         prompt.setText("Please Type a Value Below");
  81.         prompt.setTabIndex(1);
  82.         prompt.setAlignment(HorizontalAlignment.CENTER);
  83.         respond.setBackColor(Color.WINDOW);
  84.         respond.setCursor(Cursor.IBEAM);
  85.         respond.setLocation(new Point(30, 60));
  86.         respond.setSize(new Point(350, 23));
  87.         respond.setTabIndex(0);
  88.         respond.setTabStop(true);
  89.         respond.setText("I dream\'d in a dream I saw a City Invincible");
  90.         respond.setHideSelection(false);
  91.         respond.addOnKeyPress(new KeyEventHandler(this.respond_keyPress));
  92.         ok.setAnchor(ControlAnchor.BOTTOM);
  93.         ok.setLocation(new Point(160, 90));
  94.         ok.setSize(new Point(90, 30));
  95.         ok.setTabIndex(3);
  96.         ok.setTabStop(true);
  97.         ok.setText("OK");
  98.         ok.setDialogResult(DialogResult.OK);
  99.         this.setNewControls(new Control[] {
  100.             ok, 
  101.             respond, 
  102.             prompt});
  103.     }
  104.     // NOTE: End of form designer support code
  105.  
  106.     /**Change the String displayed in the prompt label
  107.      */
  108.     public void setPrompt(String promptText)
  109.     {
  110.         prompt.setText(promptText);
  111.     }
  112.     /**Determine the String displayed in the prompt label
  113.      */
  114.     public String getPrompt()
  115.     {
  116.         return prompt.getText();
  117.     }
  118.     /**Change the string displayed in the respond edit box
  119.      */
  120.     public void setResponse(String response)
  121.     {
  122.         respond.setText(response);
  123.         //TIP: this selects all the text in the textbox
  124.         respond.setSelectionStart(1);
  125.         respond.setSelectionEnd(response.length());
  126.     }
  127.     /**Determine the String displayed in the respond edit box
  128.      */
  129.     public String getResponse()
  130.     {
  131.         return respond.getText();
  132.     }
  133.     /**Sends the OK response if return is hit in the edit box (instead of clicking the OK button)
  134.      */
  135.     public void respond_keyPress(Object source, KeyEvent event)
  136.     {
  137.         if(event.keyChar==Key.RETURN)
  138.         {
  139.             setDialogResult(DialogResult.OK);
  140.         }
  141.     }
  142.     /**When the form is entered in the message loop, createHandle is called
  143.      */
  144.     public void createHandle()
  145.     {
  146.         super.createHandle();
  147.         //This next line will freeze your OS! Don't bring to front in createHandle()!
  148.         //bringToFront();
  149.     }
  150. }
  151.