R4R
Right Place For Right Person TM
 

R4R JAVAAWT ExamplesUsing Text Area in java

previous

Home

Next

Using Text Field in java
In this example we are going to show how to use Text Field.
The text field class implement a single-Line text entry area ,usually called an edit control. Text field defined the following constructor.

TextField()
: It create a default text field.
TextField(String str): It create a text field with string name.
TextField(String str,int numChar): It initializes a text field and sets its width.
To obtain the string currently contain the text field call:  getText(); . To set the text call: setText();
void getText();
void setText(String str);

The user can select a portion of the text in the text field by using:
String getSelectedText();

In our example first we implement two interface:
ActionListener();
 each listener implement the ActionLinstner.
actionPerformed(); When a option is selected or an event is occur this interface is called.

In our example we create two Text field one is name and other is password .Since text field perform their own editing function ,,your program generally will not  respond to individual key event that occur within text field. for the response you may press the enter.

//creat a Textfield
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="TextFieldDemo" width=300 height=250>
</applet>
*/
public class TextFieldDemo extends Applet implements ActionListener {
TextField name ,pass;
public void init(){
Label namep = new Label("name :",Label.RIGHT);
Label passp = new Label("password",Label.RIGHT);
name =new TextField(12);
pass =new TextField(8);
pass.setEchoChar('*');
add(namep);
add(name);
add(passp);
add(pass);
name.addActionListener(this);
pass.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
repaint();
}
public void paint(Graphics g){
g.setColor( Color.red);
g.drawString("name:"+ name.getText(),200,60);
g.drawString("selected text in name:" +name.getSelectedText(),200,80);
g.drawString("password:" + pass.getText(),200,100);
}
}

Download source code

Output of  Example

previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R