Advertisement



< Prev
Next >



JTextField class




JTextField 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 JTextField, an ActionEvent is generated. In order to handle such event, ActionListener interface should be implemented. JTextField is a component which extends TextComponent class, which further extends JComponent class.




Constructors of JTextField


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





Some methods of JTextField class


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





An example of different kinds of JTextField


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



public class TextField2
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method

}//Closing the class A


class A //implements ActionListener
{

JButton button;
JLabel label1, label2, label3, label4, label5;
JTextField field1, field2, field3, field4, field5;
String str ="";
JFrame jf;

A()
{
jf = new JFrame("Hello");
jf.setSize(440,150);

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


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

//Setting the font color to blue, in a JTextField
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);


//button.addActionListener(this);

jf.setVisible(true);
}

}

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

Figure 1



Advertisement




Handling JTextField events and reading from JTextField.


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


public class TextField3 
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A implements ActionListener
{

JButton button;
JLabel label1, label2, label3;
JTextField field1, field2, field3;
JFrame jf;


A()
{
jf = new JFrame("Handling TextField Event");
jf.setSize(400,150);

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


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

button = new JButton("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"))
{
System.out.println("Your name : " + field1.getText()+ ", Your city : " + field2.getText()+ ", Your age :" + field3.getText());
}
}

}
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 this data is read and displayed on command prompt, as soon as you press Enter Key in any of the textfield, as shown below.

Enter Key is pressed
Your name : Anuj Sobti, Your city : New York, Your age :29




Please share this article -





< Prev
Next >
< JLabel
JTextArea >



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