
How to retrieve data from text field

Hi Friend,
Try the following code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class RetrieveDataFromTextFields{
public static void main(String[] args){
JFrame f=new JFrame();
JLabel label1=new JLabel("Name: ");
JLabel label2=new JLabel("Address: ");
final JTextField text1=new JTextField(20);
final JTextField text2=new JTextField(20);
JButton b=new JButton("Submit");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value1=text1.getText();
String value2=text2.getText();
JOptionPane.showMessageDialog(null,"Name is: "+value1+"\nAddress is: "+value2);
}
});
JPanel p=new JPanel(new GridLayout(3,2));
p.add(label1);
p.add(text1);
p.add(label2);
p.add(text2);
p.add(b);
f.add(p);
f.setVisible(true);
f.pack();
}
}
Thanks

Hi Friend...
Your code is perfect & working but now if in that textfield i supplied integer numbers and want to perform any arithmatic operation on them & retrieve the result, how do i do it?
String value1=text1.getText();
Is there any way to convert value1 to integer?? Like using Convert to.int or something??

String s=t1.getText(); int a= Integer.parseInt(s);
so converted value will be in a as integer...

Thank you.... :)

i want to take out the print of the textfield output when it is dispyd in message box..in ur above program,,,,ca u help me,,??with it
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.