Advertisement



< Prev
Next >



TextField class





TextField class is used to create a textfield control, which allows a user to enter a single line text and edit it. When an enter key is pressed in a TextField, an ActionEvent is generated. In order to handle such event, ActionListener interface should be implemented. TextField is a component which extends TextComponent class, which further extends Component class.




Constructors of TextField


Constructor Description
public TextField) Creates a TextField..
public TextField(String text) Creates a TextField with a specified default text.
public TextField(int width) Creates a TextField with a specified width.
public TextField(String text, int width) Creates a TextField with a specified default text and width.



Some methods of TextField class


Methods Description
public void setText(String text) Sets a String message on the TextField.
public String getText() Gets a String message of TextField.
public void setEditable(boolean b) Sets a Sets a TextField to editable or uneditable.
public void setFont(Font f) Sets a font type to the TextField
void setForeground(Color c) Sets a foreground color, i.e. color of text in TextField.





An example of different kinds of TextField


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class TextFieldEx1
{
Button button;
Label label1, label2, label3, label4, label5;
TextField field1, field2, field3, field4, field5;
String str ="";
Frame jf;


TextFieldEx1()
{
jf = new JFrame("Hello");

label1= new Label("First Textfield");
label2= new Label("Second Textfield ");
label3= new Label("Third TextField");
label4= new Label("Fourth TextField");
label5= new Label("Fifth TextField");


field1 = new TextField();			     //calling TextField() constructor
field2 = new TextField("World peace is important"); //calling TextField(String) Constructor
field3 = new TextField(5);			     //calling TextField(int) constructor
field4 = new TextField("Smile and spread it",15);   //calling TextField(String, int) constructor

//Setting the font color to blue, in a TextField
field2.setForeground(Color.BLUE);		   

field4.setEditable(false); 			     //Making a text field uneditable
field4.setFont(new Font("Serif", Font.BOLD, 10));    //Setting a font style in a TextField

jf.setLayout(new FlowLayout());
jf.add(label1);
jf.add(field1);
jf.add(label2);
jf.add(field2);
jf.add(label3);
jf.add(field3);
jf.add(label4);
jf.add(field4);



jf.setSize(450,150);
jf.setVisible(true);
}


public static void main(String... ar)
{
new TextFieldEx1();
}
}

When you run the code, you are presented a window shown below -:

Figure 1



Advertisement




Handling TextField events and reading from TextField.


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class TextFieldEx2 implements ActionListener
{
Button button;
Label label1, label2, label3, label4;
TextField field1, field2, field3;
Frame jf;



TextFieldEx2()
{
jf = new JFrame("Handling TextField Event");
jf.setSize(340,260);

label1= new Label("Enter your name");
label2= new Label("Enter your city");
label3= new Label("Enter your age");


field1 = new TextField(20);
field2 = new TextField(20); //calling TextField(String)
field3 = new TextField(20);

button = new Button("Submit");

jf.setLayout(new FlowLayout());

jf.add(label1);
jf.add(field1);
jf.add(label2);
jf.add(field2);
jf.add(label3);
jf.add(field3);
jf.add(button);


button.addActionListener(this); //As soon as button is clicked, data from all the textfields is read
jf.setVisible(true);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getActionCommand().equals("Submit"))
{
label4= new Label("", Label.CENTER);
label4.setText("Your name : " + field1.getText()+ ", Your city : " + field2.getText()+ ", Your age :" + field3.getText());
jf.add(label4);
jf.setVisible(true);
}
}


public static void main(String... ar)
{
new TextFieldEx2();
}


}
When you run the code, you are presented a window shown in the Figure2 below -:

Figure 2


Now you may enter the data in the each textfield.

Figure 3


and as soon as you press Submit button, you are presented the data entered by you.



Please share this article -





< Prev
Next >
< Label
TextArea >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement