Sunday, July 21, 2013

TextField

TextField class allows you to create a text field to input and edit a single line of text. The TextField can generate text event and key event.

To receive text event, the application must implements TextListener interface. You will need to override the textValueChanged (TextEvent e) method of the TextListener interface to perform an action when there is any change to text in the text field. You need to use the addTextListener(TextListener listener) method to register the listener to receive the text event.

To receive key event, the application must implements KeyListener interface and register the listener.You need to use the addKeyListener (KeyListener e) method. The KeyListener interface has three methods: keyPressed(KeyEvent e), keyReleased(KeyEvent e), and keyTyped(KeyEvent e) that can be used to perform action when a key is pressed, released or typed.

Constructors of the TextField class:
-TextField()
creates an empty text field.
-TextField(int columns)
creates an empty text field with specifying the number of columns.
-TextField(String text)
creates a text field with the default text in in.
-TextField(String, int columns)
creates a text box with default text and number of columns.

Useful methods of the TextField class:
-addTextListener(TextListener listener): void
registers the text listener to receive text event from this text field.
-getText(): String
returns the text in the text field.
-setEchoChar(char c):void
sets the echo character for this text field.
-setEditable(boolean editable): void
determines whether or not the text field is editable.


Example: text field with text event

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
private Label lbl;
private TextField txtname;
InterfaceShow(String title){
setTitle(title);
setLayout(new FlowLayout(FlowLayout.CENTER));
//setExtendedState(Frame.MAXIMIZED_BOTH);
setSize(new Dimension(300,200));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
txtname=new TextField(30);
txtname.addTextListener(new TextList());
lbl=new Label();
lbl.setFont(new Font("Arial",Font.BOLD,30));
add(txtname);
add(lbl);

setVisible(true);
validate();
}

class TextList implements TextListener{
public void textValueChanged(TextEvent e){
lbl.setText(txtname.getText());
validate();
}
}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using TextField");

}
}


textfield with text event textlistener


For another example, the text field object is used with the KeyListener interface. To allow only number digits, you need to track the code of character input in the keyTyped method. The digit characters are from 0 to 9 and their equivalent codes are from 48 to 57. The consume method of the KeyEvent class is used to hide an undesirable character.

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
private Label lbl;
private TextField txtname;
InterfaceShow(String title){
setTitle(title);
setLayout(new FlowLayout(FlowLayout.CENTER));
//setExtendedState(Frame.MAXIMIZED_BOTH);
setSize(new Dimension(300,200));
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
txtname=new TextField(30);
txtname.addKeyListener(new KeyList());
lbl=new Label("Enter a number:");
add(lbl);
add(txtname);
setVisible(true);
validate();
}

class KeyList implements KeyListener{
public void keyTyped(KeyEvent e){
int intkey=e.getKeyChar();
if(!(intkey>=48 && intkey<=57))
e.consume();
}
public void keyPressed(KeyEvent e){

}
public void keyReleased(KeyEvent e){

}

}
}
public class AWTControls {
public static void main(String[] args){
new InterfaceShow("Using TextField");

}
}


text field with key event keylistener

1 comment:

  1. Awesome! keep it up your work. you are providing precious information to users.

    amrapali kata ias wiki

    ReplyDelete