Saturday 15 October 2016

JTextField


  JTextField allows us to edit one line of text. It is derived from JTextComponent, which provides the basic functionality common to Swing text components.. JTextField uses the Document interface for its model. 
    The three most widely used JTextField constructors are as follows:
JTextField(int cols)
JTextField(String str , int cols)
JTextField(String str)
      Here, str is the string to be initially presented, and cols in the number of columns in the text field. If no string is specified, the text field is initially empty. If the number of columns is not specified, the text field is sized to fit the specified string. 
      JTextField generates events in response to user interaction. For example, an ActionEvent is fired when the user presses ENTER. A CaretEvent is fired each time the caret (i.e. the cursor) changes position. 
   We can obtain the text currently in the text field by calling getText().  
 Example:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyTextField extends JApplet{
  JTextField t1;
  public void init()  {
   try  {
    SwingUtilities.invokeAndWait(new Runnable() {
      public void run()   {
          makeGUI();
      }
    });
  } catch(Exception e){
           System.out.println("Sorry some error occured "+e);
        }
 }
 private void makeGUI() {
   setLayout(new FlowLayout());
   t1=new JTextField(15);
   add(t1);
   t1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent ae){  
      showStatus(t1.getText());
    }
  });
  }
}



                                                                         Output:

No comments:
Write comments