home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-1dot1 / TextDemo.java < prev   
Encoding:
Java Source  |  1997-07-13  |  3.4 KB  |  117 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20.  
  21. public class TextDemo extends Applet 
  22.                       implements ActionListener {
  23.     TextField textField;
  24.     TextArea textArea;
  25.     TextArea displayArea;
  26.  
  27.     public void init() {
  28.     Button button = new Button("Clear");
  29.     button.addActionListener(this);
  30.  
  31.     textField = new TextField(20);
  32.     textField.addActionListener(new MyTextActionListener());
  33.     textField.addTextListener(new MyTextListener("Text Field"));
  34.  
  35.         textArea = new TextArea(5, 20);
  36.     textArea.addTextListener(new MyTextListener("Text Area"));
  37.  
  38.     displayArea = new TextArea(5, 20);
  39.     displayArea.setEditable(false);
  40.  
  41.     GridBagLayout gridbag = new GridBagLayout();
  42.     GridBagConstraints c = new GridBagConstraints();
  43.     setLayout(gridbag);
  44.     c.fill = GridBagConstraints.BOTH;
  45.     c.weightx = 1.0;
  46.  
  47.     /*
  48.      * Hack to get around gridbag's refusal to allow
  49.      * multi-row components in anything but the left column.
  50.      */
  51.     Panel leftPanel = new Panel();
  52.     leftPanel.setLayout(new BorderLayout());
  53.     leftPanel.add("North", textField);
  54.     leftPanel.add("Center", textArea);
  55.  
  56.     c.gridheight = 2;
  57.     gridbag.setConstraints(leftPanel, c);
  58.     add(leftPanel);
  59.  
  60.     c.weighty = 1.0;
  61.     c.gridwidth = GridBagConstraints.REMAINDER;
  62.     c.gridheight = 1;
  63.     gridbag.setConstraints(displayArea, c);
  64.     add(displayArea);
  65.  
  66.     c.weighty = 0.0;
  67.     gridbag.setConstraints(button, c);
  68.     add(button);
  69.  
  70.     textField.requestFocus();
  71.     }
  72.  
  73.     class MyTextListener implements TextListener {
  74.     String preface;
  75.  
  76.     public MyTextListener(String source) {
  77.         preface = source
  78.               + " text value changed.\n"
  79.               + "   First 10 characters: \"";
  80.     }
  81.  
  82.         public void textValueChanged(TextEvent e) {
  83.         TextComponent tc = (TextComponent)e.getSource();
  84.         String s = tc.getText();
  85.         try {
  86.         s = s.substring(0, 10);
  87.         } catch (StringIndexOutOfBoundsException ex) {
  88.         }
  89.  
  90.         displayArea.append(preface + s + "\"\n");
  91.  
  92.             //Scroll down, unless the peer still needs to be created.
  93.         if (displayArea.isValid()) {
  94.             displayArea.setCaretPosition(java.lang.Integer.MAX_VALUE);
  95.         } 
  96.     }
  97.     }
  98.  
  99.     class MyTextActionListener implements ActionListener {
  100.         /** Handle the text field Return. */
  101.         public void actionPerformed(ActionEvent e) {
  102.         int selStart = textArea.getSelectionStart();
  103.         int selEnd = textArea.getSelectionEnd();
  104.  
  105.             textArea.replaceRange(textField.getText(),
  106.                               selStart, selEnd);
  107.             textField.selectAll();
  108.         }
  109.     }
  110.  
  111.     /** Handle button click. */
  112.     public void actionPerformed(ActionEvent e) {
  113.     displayArea.setText("");
  114.     textField.requestFocus();
  115.     }
  116. }
  117.