Reputation: 1837
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 .
Upvotes: 1
Views: 1388
Reputation: 285403
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());
Upvotes: 5