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?
Asked
Active
Viewed 4.1e+01k times
44
-
13Did you try reading the javadoc? – Stephen C Apr 22 '11 at 04:37
-
2It is a duplicate. Maybe not of a single question. But this is not a new problem. [Getting text](http://stackoverflow.com/questions/3885577/retrieve-jtextfield-text-value) & [Adding action listener](http://stackoverflow.com/questions/4062195/adding-an-action-listener-to-a-jcombobox). Plus I am sure if you would google it there is even an example for what you describe. – Boro Apr 22 '11 at 06:21
-
Its very easy to get value from JTextfield.. try to read java doc........it will help you to develop programs.. – Chetan Jan 30 '15 at 08:57
7 Answers
70
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 ...
}
})

Harry Joy
- 58,650
- 30
- 162
- 207
10
* First we declare JTextField like this
JTextField testField = new JTextField(10);
* We can get textfield value in String like this on any button click event.
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
String getValue = testField.getText()
}
})

Chetan
- 184
- 1
- 8
8
How do we retrieve a value from a text field?
mytestField.getText();
ActionListner
example:
mytextField.addActionListener(this);
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
textArea.append(text + newline);
textField.selectAll();
}

trashgod
- 203,806
- 29
- 246
- 1,045

Nirmal- thInk beYond
- 11,847
- 8
- 35
- 46
-
1
-
4So maybe something like `String newline = System.getProperty("line.separator");` would be correct? – trashgod Apr 22 '11 at 07:03
4
What I found helpful is this condition that is below.
String tempEmail = "";
JTextField tf1 = new JTextField();
tf1.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent evt){
tempEmail = ((JTextField)evt.getSource()).getText() + String.valueOf(evt.getKeyChar());
}
});

Nathan Tuggy
- 2,237
- 27
- 30
- 38

ArifMustafa
- 4,617
- 5
- 40
- 48
-
There is one problem: the control keys. they will get printed as soon as you hit them. For example, CONTROL, ALT, BACK, etc. All those keys will get printed in your string. – Mesabloo May 13 '18 at 10:40
4
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swingtest extends JFrame implements ActionListener
{
JTextField txtdata;
JButton calbtn = new JButton("Calculate");
public Swingtest()
{
JPanel myPanel = new JPanel();
add(myPanel);
myPanel.setLayout(new GridLayout(3, 2));
myPanel.add(calbtn);
calbtn.addActionListener(this);
txtdata = new JTextField();
myPanel.add(txtdata);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == calbtn) {
String data = txtdata.getText(); //perform your operation
System.out.println(data);
}
}
public static void main(String args[])
{
Swingtest g = new Swingtest();
g.setLocation(10, 10);
g.setSize(300, 300);
g.setVisible(true);
}
}
now its working

jayesh
- 2,422
- 7
- 44
- 78
-
Reformatted code; please revert if incorrect. Unfortunately, this does not compile. – trashgod Apr 22 '11 at 06:28
-
Please don't shout; it makes you sound angry. Even ignoring the missing imports, it looks like you're missing the constructor. Can I help you fix it? – trashgod Apr 22 '11 at 06:57
-
-
No, this is worse; I'm trying to help improve this answer. You appear to be declaring `class serverfact` and instantiating something named `VIEWBTN`. Neither name uses a style with which I am familiar. – trashgod Apr 22 '11 at 07:12
-
haha ... that's cool, commanding the upvote ;-) BTW: you should stick to java naming conventions – kleopatra Apr 22 '11 at 10:20
-
@kleopatra is right about the naming conventions: they improve readability, e.g. `class ServerFact` or `JTextField resText`. I previously down-voted this answer because it was misleading; it is correct now, so I have reversed the vote. I'm not sure it adds anything to prior answers, but I defer to @harshini on whether it is useful. I'd be happy to examine other answers you have proposed. I can't promise an up-vote, but I can promise honest, constructive review. – trashgod Apr 22 '11 at 11:12
2
Just use event.getSource()
frim within actionPerformed
Cast it to the component
for Ex, if you need combobox
JComboBox comboBox = (JComboBox) event.getSource();
JTextField txtField = (JTextField) event.getSource();
use appropriate api to get the value,
for Ex.
Object selected = comboBox.getSelectedItem(); etc.

Andrey Korneyev
- 26,353
- 15
- 70
- 71

Anuj Singh
- 21
- 1
0
You can use the getText() method anywhere in your code it is instancely called by your object, So you can use the method anywhere within a calass

Melvin Infant
- 9
- 1