Troubleshooting

   

Restricting text characters

How can I restrict the characters that a user enters in a text field or a text area?

Restricting what characters a user can input is accomplished in a manner that is very similar to restricting the total number of characters that are input ”as shown in the TextListenerDemo application. To see how this is accomplished, the following textValueChanged method prevents a user from entering lowercase letters :

 public void textValueChanged (TextEvent e) {    TextField tf = (TextField) e.getSource ();    StringBuffer text = new StringBuffer (tf.getText ());    int cp = tf.getCaretPosition ();    if (cp == 0)        return;    char ch = text.charAt (cp - 1);    if (Character.isLowerCase (ch))    {        ch = Character.toUpperCase (ch);        text.setCharAt (cp - 1, ch);        tf.setText (text.toString ());        tf.setCaretPosition (cp);    } } 

Dynamically Adding and Removing Components

How do I dynamically add components to and remove components from a GUI?

You can call any one of Container 's overloaded add methods to add components, and call its remove method to remove components. This situation is demonstrated in the following source code to the CARDemo application.

 // CARDemo.java import java.awt.*; import java.awt.event.*; class CARDemo extends Frame implements ActionListener {    final static int USE_BUTTON = 0;    final static int USE_LIST = 1;    final static int USE_TEXTAREA = 2;    final static int USE_TEXTFIELD = 3;    int use = USE_TEXTAREA;    Component c;    CARDemo (String title)    {       super (title);       addWindowListener (new WindowAdapter ()                          {                              public void windowClosing (WindowEvent e)                              {                                 System.exit (0);                              }                          } );       switch (use)       {          case USE_BUTTON:             c = new Button ("Ok");             break;          case USE_LIST:             c = new List ();             ((List) c).add ("First");             ((List) c).add ("Second");             ((List) c).select (0);             break;          case USE_TEXTAREA:             c = new TextArea ();             break;          case USE_TEXTFIELD:             c = new TextField ();       }       add (c);       MenuBar mb = new MenuBar ();       Menu m = new Menu ("File");       m.addActionListener (this);       m.add ("Add");       m.add ("Remove");       m.addSeparator ();       m.add ("Exit");       mb.add (m);       setMenuBar (mb);       pack ();       setVisible (true);    }    public void actionPerformed (ActionEvent e)    {       if (e.getActionCommand ().equals ("Exit"))          System.exit (0);       if (e.getActionCommand ().equals ("Add"))       {           add (c);           c.requestFocus ();       }       else           remove (c);    }    public static void main (String [] args)    {       new CARDemo ("Component Add/Remove Demo");    } } 

CARDemo presents a GUI with a File menu and either a button, list, text area, or text field component (depending on which " USE " constant is assigned to the use variable). If Add is selected from the File menu, the appropriate component is added to the GUI by calling the add method. However, if Remove is selected, the component is removed by calling the remove method.

When add is called, it (internally) removes the component from the container. This is why you don't see multiple copies of the newly added component. The component's requestFocus method is then called to ensure that the added component has the focus.

Note

When you add a text area to the GUI, you might notice a NullPointerException object being thrown. This exception is related to internal logic and appears to be the result of a Java bug (because this exception is never thrown when either a button, list, or text field component is added). However, the thrown NullPointerException object doesn't disrupt execution of CARDemo .


If you run CARDemo , type some text in either the text area or text field component (depending on which component is identified by the use variable), remove this component, and then add the component; you'll notice that the text reappears. This proves that component removal results in only removing its peer: The component's object still exists. When the component is added at a later time, the information in this object is used to restore the peer to previous settings.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net