go to previous page   go to home page   go to next page

Answer:

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

GUI

Nothing happens because the lower text field does not have a registered listener. You could modify the program so it 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 text field. This can be done with the setEditable() method:

text.setEditable( false );

Here text is a variable that refers to a JTextField. The setEditable() method has one Bolean parameter. 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 JFrame implements ActionListener
{

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

   JLabel outLabel   = new JLabel( "Here is Your Name :" ) ;
   TextField outText = new TextField( 15 );

   public Repeater( String title)      // constructor
   {  
      super( title );
      setLayout( new FlowLayout() ); 

      add( inLabel  ) ;
      add( inText   ) ;
      add( outLabel ) ;
      add( outText  ) ;

      inText.addActionListener( this );
      setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
   }
   
   .....
}

QUESTION 13:

Suggest a place to use the setEditable() method.