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?
Sample code can be: button. addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String textFieldValue = testField. getText(); // .... do some operation on value ... } })
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.
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.
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 ... } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With