• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to apply actionperformed to Jtextfields in java ?

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to our GUI forum.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 650
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant to add that you can have a look at the tutorials for action listeners to see how they are done.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
It's a tiny ad only because the water is so cold.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic