Input fields helps in taking the input from the user and makes it visible on the given area. AWT Input Fields chapter explains about various inputting fields like:
TextField
TextArea
Textfield
Description
Textfield is used to accept a single-line input from the end-user and convert it to an event to produce a proper output upon confirmation.
Declaration:
java.awt.TextField class can be declared as follows:
public class TextField extends TextComponent
Example
[java]import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class SPlesson extends Applet implements ActionListener
{
TextField txtFld1 = new TextField(20);
TextField txtFld2 = new TextField(20);
public void init()
{
add(txtFld1);
add(txtFld2);
txtFld1.addActionListener(this);
txtFld2.addActionListener(this);
}
public void actionPerformed(ActionEvent theEvent)
{
if (theEvent.getSource() == txtFld1)
{
System.out.println("txtFld1");
}
else
{
System.out.println("txtFld2");
}
}
}[/java]
Output
TextArea
Description
Unlike Textfield, TextArea supports multi-line input from the end-user. When the content given is more than the given space, scroll bar appears to extend the space.
Declaration:java.awt.TextArea class can be declared as follows:public class TextArea extends TextComponent
Examples
[java]
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.TextListener;
import java.awt.event.TextEvent;
public class SPlesson extends Frame implements TextListener
{
TextArea typeText, displayText;
public SPlesson()
{
super("Practicing Text Area");
// default BorderLayot is used
typeText = new TextArea("Type Here", 10, 20);
displayText = new TextArea();
displayText.setRows(10);
displayText.setColumns(20);
typeText.addTextListener(this);
add(typeText, BorderLayout.NORTH);
add(displayText, BorderLayout.SOUTH);
setSize(300, 350);
setVisible(true);
}
public void textValueChanged(TextEvent e)
{
String str = typeText.getText();
displayText.setText(str);
}
public static void main(String args[])
{
new SPlesson();
}
}[/java]
Output