Java Tutorial

AWT Text Fields

TextField is GUI interactive component which allows entering the data in single line. Whatever the data, we enter to text field, by default that data will be treated as string.

If we are able to read the data of text field then that text field is known as editable or non-echo or normal text field.

If we are unable to read the data of the text field then that text field is known as echo text field and the character which is available is known as echo character.

Creating a text box is nothing but creating an object of TextField class.

Text Field API:

Constructors:

TextField ();
TextField (int);// int represents size of the text field
TextField (String);// data in the text field 
TextField (String, int);

Instance methods:

public void setSize (int); 
public int getSize ();  
public void setText (String); 
public String getText ();
public void setEchoChar (char); 
public char getEchoChar ();
public void setEditable (boolean);
public boolean getEditable(); 
public boolean isEditable ();
public void addTextListener (TextListener); 
public void removeTextListener (TextListener);

Write a java program which illustrates the concept of TextField?

Answer:

import java.awt.*;
 import java.awt.event.*;
class Textfield extends Frame
{
    Label l1, l2, l3; 
    TextField tf1, tf2, tf3; 
    Button b1, b2, b3, b4; 
    Textfield ()
    {
        setTitle ("Operations"); 
        setSize (200, 200);
        setLayout (new FlowLayout ()); 
        l1=new Label ("ENTER FIRST NUMBER"); 
        l2=new Label ("ENTER SECOND NUMBER");
        l3=new Label ("ENTER THIRD NUMBER");
        tf1=new TextField (15);
        tf2=new TextField (15);
        tf3=new TextField (15);
        b1=new Button ("Sum");
        b2=new Button ("Sub"); 
        b3=new Button ("Mul");
        b4=new Button ("Exit"); 
        add (l1);
        add (l2);
        add (l3);
        add (tf1);
        add (tf2);
        add (tf3);
        add (b1);
        add (b2);
        add (b3);
        add (b4);
        b1.addActionListener (new al());
        b2.addActionListener (new al()); 
        b3.addActionListener (new al()); 
        b4.addActionListener (new al()); 
        setVisible (true);
    }
    class al implements ActionListener
    {
        public void actionPerformed (ActionEvent ae)
        {
            String cap=ae.getActionCommand (); 
            if (cap.equalsIgnoreCase ("Sum"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();  
                int n1=Integer.parseInt (s1); 
                nt n2=Integer.parseInt (s2); 
                int n3=n1+n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Sub"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();  
                int n1=Integer.parseInt (s1); 
                int n2=Integer.parseInt (s2); 
                int n3=n1-n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Mul"))
            {
                String s1=tf1.getText (); 
                String s2=tf2.getText ();
                int n1=Integer.parseInt (s1);
                int n2=Integer.parseInt (s2);
                int n3=n1*n2;
                String s=String.valueOf (n3); 
                tf3.setText (s);
            }
            if (cap.equalsIgnoreCase ("Exit"))
            {
                System.exit (0);
            }
        }
    }
};

class TextfieldDemo
{
    public static void main (String [] args)
    {
        new Textfield ();
    }
};

Write a java program to close the window of awt?

Answer:

import java.awt.*;
import java.awt.event.*; 
class CloseWin extends Frame
{
    CloseWin ()
    {
        setTitle ("Java Example"); 
        setBackground (Color.green); 
        setSize (200, 200);
        this.addWindowListener (new WinAdap ()); 
        setVisible (true);
    }
    class WinAdap extends WindowAdapter
    {
        public void windowClosing (WindowEvent we)
        {
            System.exit (0);
        }
    }
};

class CloseWinDemo
{
    public static void main (String [] args)
    {
        new CloseWin ();
    }
};

Adapter class:

It is one which contains null body definition for those methods which are inheriting from appropriate Listener.

In java.awt.event.* we have Listener interface called Widow Listener which contains seven abstract methods. In the derived class implements Window Listener interface; it is mandatory for derived class to define all the methods even though the derived class is not required. If the derived class wants to define the required methods, it has to extend its corresponding adapter class called java.awt.event.WindowAdapter and this class contains null body definition for Window Listener interface methods.

Therefore which our Listener interface contains more than one undefined method for that Listener interfaces we have the appropriate adapter class whose general notation is XXXAdapter.

For example:

ListenerAdapter
WindowListenerWindowAdapter
ActionListener ActionAdapter
MouseListenerMouseAdapter
MouseMotionListenerMouseMotionAdapter