wood burning stoves*
The moose likes Swing / AWT / SWT and the fly likes how to apply actionperformed to Jtextfields in java ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "how to apply actionperformed to Jtextfields in java ?" Watch "how to apply actionperformed to Jtextfields in java ?" New topic
Author

how to apply actionperformed to Jtextfields in java ?

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

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.


OCPJP 6.0 93%
OCPJWCD 5.0 98%
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.

Luck!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
Cross posted: http://stackoverflow.com/questions/3126114/how-to-apply-actionperformed-to-jtextfields-in-java

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.

Be Forthright When Cross Posting To Other Sites


Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2180
    
    7
Another option is adding a document listener to the JTextField.


So much for warning you about the cross posting so you don't waste time making suggestions that have been made hours ago.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I'm reading this thread, not the other one. If you don't want people to respond to this thread, perhaps you should have it locked?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634




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
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
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?
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
I meant to add that you can have a look at the tutorials for action listeners to see how they are done.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to apply actionperformed to Jtextfields in java ?