What happends if you enter text into the lower TextField and hit enter?

A good answer might be:

The user can enter text into the lower box, and edit it using the arrow keys and other keys of the keyboard. But when the user hits enter, nothing happens.

The setEditable() Method

Nothing happens because the lower TextField does not have a registered listener. You could modify the program so this TextField has a listener, but this would not follow the original design for the program. A better idea (for this program) is to prevent the user from entering text into the wrong TextField.

This can be done with a TextField's setEditable() method:


text.setEditable( false );
Here text is a variable that refers to a TextField. The setEditable() method has one parameter that evaluates to either true or false. If the parameter is false, then the field can not be altered by the user (the setText() method still works, however.) Here is an excerpt from the program:

public class Repeater extends Frame implements ActionListener
{

   Label inLabel     = new Label( "Enter Your Name:  " ) ;
   TextField inText  = new TextField( 15 );

   Label outLabel    = new Label( "Here is Your Name :" ) ;
   TextField outText = new TextField( 15 );
   
   Repeater()                              // constructor
   {  
      setLayout( new FlowLayout() );       // choose the layout manager 
      add( inLabel  ) ;
      add( inText   ) ;
      add( outLabel ) ;
      add( outText  ) ;

      inText.addActionListener( this );
   }

   . . . . . .
}

QUESTION 12:

Suggest a place to use the setEditable() method.

Click Here after you have answered the question