i am buiding a small jframe with 2 lables and 2 textfields(Jtextfield1,Jtextfield2) and a jbutton .i want that when the user enters some data in the Jtextfield1,the data is retrieved from database regarding user and his details are displayed in the Jtextfield2. i know how to retrieve data from database but how to apply actionperformed to Jtextfield1.please help and give an example too.
You can give a JTextField an ActionListener same as a JButton with addActionListener(...). This ActionListener's actionPerformed method will be called if and only if <enter> has been pressed when the JTextField has the focus. Other options include giving the JTextField a FocusListener via addFocusListener(...) and having the code be called in the focusLost method of this listener, or (and my preference) giving the user a JButton to press to trigger the action and retrieval from the database. And for examples on how to do these things, your best checking out the Sun Swing tutorials. As always, if your code doesn't work the way you want it to, please feel free to post it here and we'll likely give it a look.
I see I wasted my time answering the question in the orther forum. Not only did you cross post the question, you didn't even post the question in the proper forum, so I didn't know the question was already answered.
Another option is adding a document listener to the JTextField. This way you can watch each change to the text value (even as the user is entering characters) and update your GUI accordingly. As mentioned, the tutorials show examples of their use.
i want that when user enters some string in the jtextfield2 and presses enter,it should be displayed in jTextfield3
i tried it but it's not working.
please help
When did you want to copy the contents of the text field? As the user types the characters, when the user moves out of the field, or when the user hits the carriage return while the text field has the keyboard focus?
It looks like you've added an action listener to your field, but what is the code doing when that listener is activated?
Well, yes, that's true, your code doesn't do that. When Enter is pressed in your badly-named "jTextField2", the method "jTextField2ActionPerformed" is called. And the entire body of that method is this:
As I'm sure you know, that's a comment. You would need to add some real code which does whatever you want it to do.
We don't have time to be charming! Quick, read this tiny ad: