Wings
Wings

Reputation: 75

How to pass data from a JTextField to another JTextField

I need to pass a text from a JTextField in a JFrame to a second JTextField in another JFrame. GUI has been created using GUI Editor netbeans 6.9.

Upvotes: 1

Views: 3934

Answers (3)

Kai Mechel
Kai Mechel

Reputation: 753

You should bind your model to both of the views (JTextFields) instead of copying Text from one to another.

Read http://www.martinfowler.com/eaaDev/OrganizingPresentations.html for more information.

Upvotes: 1

dogbane
dogbane

Reputation: 274532

Use textField.getText to get the text from a textfield and anotherTextField.setText to set the text of the other textfield.

If your JFrames are in two different classes you may have to expose methods in those classes to get and set the text between them.

Upvotes: 2

jzd
jzd

Reputation: 23629

  1. Don't use a GUI Editor, especially if you are just learning Swing.
  2. You need a reference saved somewhere to both fields so that you getText() of the first JTextField and setText() on the second field.

Upvotes: 4

Related Questions