home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-02-24 | 4.3 KB | 151 lines |
-
-
- /* Copyright (C) Microsoft Corporation, 1996-1998. All rights reserved.
-
- This source code is intended only as a supplement to Microsoft
- Visual J++ 6.0. See this product's on-line documentation for detailed
- information regarding Microsoft code samples.
-
- */
-
- import wfc.app.*;
- import wfc.core.*;
- import wfc.ui.*;
-
- /**To use an InputBox:
- * 1. create one using a constructor
- * a. When using the default constructor, use setText(String title) to set
- * the window text; use setResponse(String response) to set the default Response;
- * use setPrompt(String promptText) to set the prompt
- * 2. show the InputBox as a dialog
- * a. e.g. InputBox warmGun = new InputBox();
- * warmGun.showDialog();//see next point -- the integer this returns is useful
- * 3. take the text from the edit box using getResponse()
- * a. e.g. if(warmGun.showDialog()==DialogResult.OK)
- * happiness = warmGun.getResponse();
- * else
- * throw new IllegalArgumentException("One must hit \'OK\' after entering a value")
- */
- public class InputBox extends Form
- {
- /**Default Constructor -- useable
- */
- public InputBox()
- {
- initForm();
- }
- /**Make an InputBox with a custom prompt and no default response
- */
- public InputBox(String promptText)
- {
- this();
- setPrompt(promptText);
- setResponse(null);
- }
- /**Make an InputBox with a custom prompt, title, and default response
- */
- public InputBox(String promptText, String title, String defaultResponse)
- {
- this();
- prompt.setText(promptText);
- setText(title);
- setResponse(defaultResponse);
- }
-
-
- /**
- * NOTE: The following code is required by the Visual J++ form
- * designer. It can be modified using the form editor. Do not
- * modify it using the code editor.
- */
- Container components = new Container();
- Label prompt = new Label();
- Edit respond = new Edit();
- Button ok = new Button();
-
- private void initForm()
- {
-
- this.setAnchor(ControlAnchor.BOTTOM);
- this.setBackColor(Color.CONTROL);
- this.setLocation(new Point(0, 0));
- this.setSize(new Point(416, 154));
- this.setTabIndex(-1);
- this.setTabStop(true);
- this.setText("Visual J++ 6.0 Sample Code");
- this.setAutoScaleBaseSize(16);
- this.setClientSize(new Point(408, 127));
- prompt.setLocation(new Point(30, 10));
- prompt.setSize(new Point(350, 30));
- prompt.setText("Please Type a Value Below");
- prompt.setTabIndex(1);
- prompt.setAlignment(HorizontalAlignment.CENTER);
- respond.setBackColor(Color.WINDOW);
- respond.setCursor(Cursor.IBEAM);
- respond.setLocation(new Point(30, 60));
- respond.setSize(new Point(350, 23));
- respond.setTabIndex(0);
- respond.setTabStop(true);
- respond.setText("I dream\'d in a dream I saw a City Invincible");
- respond.setHideSelection(false);
- respond.addOnKeyPress(new KeyEventHandler(this.respond_keyPress));
- ok.setAnchor(ControlAnchor.BOTTOM);
- ok.setLocation(new Point(160, 90));
- ok.setSize(new Point(90, 30));
- ok.setTabIndex(3);
- ok.setTabStop(true);
- ok.setText("OK");
- ok.setDialogResult(DialogResult.OK);
- this.setNewControls(new Control[] {
- ok,
- respond,
- prompt});
- }
- // NOTE: End of form designer support code
-
- /**Change the String displayed in the prompt label
- */
- public void setPrompt(String promptText)
- {
- prompt.setText(promptText);
- }
- /**Determine the String displayed in the prompt label
- */
- public String getPrompt()
- {
- return prompt.getText();
- }
- /**Change the string displayed in the respond edit box
- */
- public void setResponse(String response)
- {
- respond.setText(response);
- //TIP: this selects all the text in the textbox
- respond.setSelectionStart(1);
- respond.setSelectionEnd(response.length());
- }
- /**Determine the String displayed in the respond edit box
- */
- public String getResponse()
- {
- return respond.getText();
- }
- /**Sends the OK response if return is hit in the edit box (instead of clicking the OK button)
- */
- public void respond_keyPress(Object source, KeyEvent event)
- {
- if(event.keyChar==Key.RETURN)
- {
- setDialogResult(DialogResult.OK);
- }
- }
- /**When the form is entered in the message loop, createHandle is called
- */
- public void createHandle()
- {
- super.createHandle();
- //This next line will freeze your OS! Don't bring to front in createHandle()!
- //bringToFront();
- }
- }
-