1

I do accept that this is a dumb question to many of you! First of all i want to say that my java knowledge is low as far as graphical user interface is concerned! I have a problem with textFields. I want to get the value of one JTextField object and display it in another JTextField object!This is what i tried but it is not working!

       textField2.setText(textField1.getText());

The problem is that i have two frames objects and each has a textField object and i want to copy one value from jTextField1 of jFrame1 to jTextField2 of jFrame2 .

1
  • 2
    How is it not working? At what point in your code is this snippet running? Are you sure there is some text in textField1 at the point that this is running? Commented Feb 1, 2012 at 15:34

1 Answer 1

5

The question is valid, the amount of information that you provide is not. There's no way to help you til you tell enough details so that we can understand what's wrong.

Putting my mind-reading hat on though, I am going to guess that your problem is that you make this method call above on program start up, and expect that if you update one JTextField, the other will be updated automatically, but that's not so. When you make this call:

textField2.setText(textField1.getText());

All you're doing is placing the String held in the first field into the second field. On program start up, this may be null, but even if it contained text, the String is immutable and will never change, even if the 1st field's text changes.

If you want one field to always hold the same text as the other, have them share the same model:

textField2.setDocument(textField1.getDocument()); // * edited per mKorbel's rec

If your goal on the other hand is to get the text from one JTextField and put it into another but only when the user chooses to do this, then use an ActionListener that is added to either a JButton or to the first JTextfield itself (which is activated by pressing enter while the caret is in the field), and in that listener, place your line of code:

textField2.setText(textField1.getText());
Sign up to request clarification or add additional context in comments.

3 Comments

@mKorbel: that's what happens when I post code without testing it. No "maybes" about your recommendation -- Thanks!!!
both we have thanking to (@trashgod), btw and I need this info on today, that reason for quick reply,
@Xris: you're welcome. If you want to learn to program Swing, ditch the NetBeans-generated code, go to the Swing tutorials, and learn to code by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.