Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Retrieve value from JTextField in Java Swing?

Tags:

java

swing

How do we retrieve value from a textfield and actionPerformed()? I need the value to be converted into String for further processing. I have created a textfield on clicking a button I need to store the value entered into a String can you please provide a code snippet?

like image 829
harshini Avatar asked Apr 22 '11 04:04

harshini


People also ask

How do I get TextField value in swing?

Sample code can be: button. addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String textFieldValue = testField. getText(); // .... do some operation on value ... } })

What method is used to read the text from a JTextField?

Notice the use of JTextField 's getText method to retrieve the text currently contained by the text field. The text returned by this method does not include a newline character for the Enter key that fired the action event.

What is JTextField in Java Swing?

JTextField is a lightweight component that allows the editing of a single line of text. For information on and examples of using text fields, see How to Use Text Fields in The Java Tutorial. JTextField is intended to be source-compatible with java. awt. TextField where it is reasonable to do so.


1 Answers

testField.getText() 

See the java doc for JTextField

Sample code can be:

button.addActionListener(new ActionListener(){    public void actionPerformed(ActionEvent ae){       String textFieldValue = testField.getText();       // .... do some operation on value ...    } }) 
like image 98
Harry Joy Avatar answered Sep 21 '22 06:09

Harry Joy