Advertisement
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. |
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. |
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);
}
}
Advertisement
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 -:
Enter Key is pressed
Your name : Anuj Sobti, Your city : New York, Your age :29
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement