# JTextFields, setText() and getText(
![[lblandtext.png]]
The following code produces the above GUI. Whatever is typed in the JTextField is put into the JLabel.
Note the global scope of lbl and txt, and the use of setText() and getText().
```java
public class MyFrame extends JFrame implements ActionListener {
JLabel lbl;
JTextField txt;
MyFrame() {
this.setSize(250,150);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container vert = Box.createVerticalBox();
lbl = new JLabel("Nothing input yet...");
vert.add(lbl);
txt = new JTextField("Type in text and press Enter...");
vert.add(txt);
this.add(vert);
txt.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String s = txt.getText();
lbl.setText(s);
}
}
```
# Reading Numbers from a JTextField
You may have realised that the getText() method only returns a String.
Suppose the user enters a number that you want to perform a calculation on. You may need to convert the String to int. Here's how it's done:
```java
String s = txt.getText();
int i = Integer.parseInt(s);
```
You can also convert a String to a double:
```java
String s = txt.getText();
double i = Double.parseDouble(s);
```
and so on.
You can convert an int to a String by using
```java
String s = Integer.toString(32);
or
String s = Double.toString(32.1);
```
# Note
Integer and Double are known as **class wrappers**. They put a class around the **primitives** int and double.