Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to copy data from one Jtextfield into another JTextfield

Status
Not open for further replies.

tig12

Programmer
Feb 27, 2007
20
0
0
GB
Hi,

I don't really know how to copy text that is in a Jtextfield to another JTextfield. For example, if I have a JTextfield where users enter an account number. And I want to copy this account number into another JTextField that is on another page i.e. Frame. How would I do this?

goal copy from one jtextfield to another jtextfield.

Thanks in advance

 
Assuming that both jtextfields are declared globally, why not JTextField <second text field> = new JTextField(<first text field>.getText();?

_________________
Bob Rashkin
 
When do you want to copy the values?
When the user writes some text in the first one?
When he clicks on a button?

If it's the second case (clicking on a button), the button needs an ActionListener, that is aware that this button has been clicked.
This ActionListener needs to know about both the JTextFields.

So what I'd do is to write my own class MyActionListener that extends ActionListener.
In the class that creates both Frames I would create MyActionListener, passing both frames as argument (in one frame there is the first JTextField and the JButton, and in the second frame there is the other JTextField).
Then I would add myActionListener to the button as ActionListener.
In MyActionListener, in the method actionPerformed I'd do:
Code:
if (actionEvent.getSource()==firstFrame.button)
{
   secondFrame.textField.setText(firstFrame.textField.gettext());
}

 
thank you for the quick response
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top