Results 1 to 6 of 6
  1. #1
    sasi.tati is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Thumbs up [SOLVED]How to handle after clicking Enter key

    Dear All

    I had a form(in swing) which is having a text field and a button, after entering some number in to this text field and after clicking the button the program is executing fine, if i try to click the enter key after entering number in the text field, i am unable to proceed further.

    Can any one guide how to overcome from this, i tried with key events but it is not working perfectly. How to handle after clicking the Enter key i have to do the same functionality as of the button existing in that form.

    Thanks in Advance waiting for reply.
    Last edited by sasi.tati; 10-25-2010 at 10:02 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One solution is to add an ActionListener to the JTextField, and in fact I'd add the very same ActionListener that you add to the JButton. This listener is activated by pressing enter when in the text field. What you don't want to do is to fiddle with key listeners.

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    Add a ActionListener to the textfield. You could use the same one which was added to the button!
    Because:
    "A text field is a basic text control that enables the user to type a small amount of text. When the user indicates that text entry is complete (usually by pressing Enter), the text field fires an action event."
    --> How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    Java Code:
    		ActionListener listener = new ActionListener() {
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				//todo
    			}
    		};
    		button.addActionListener(listener);
    		field.addActionListener(listener);
    Last edited by eRaaaa; 10-23-2010 at 04:29 PM.

  4. #4
    sasi.tati is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Default

    Thank you very much for your kind response, i will try in this way and let u know.

  5. #5
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    The simplest seems me to register the button as default button. Then a enter on every place will work like a click.
    Volker Berlin
    www.inetsoftware.de

  6. #6
    sasi.tati is offline Member
    Join Date
    Jul 2010
    Posts
    18
    Rep Power
    0

    Thumbs up

    [SOLVED]Thank you very much i got the solution with the ActionListner...
    Last edited by sasi.tati; 10-25-2010 at 10:01 AM.

Similar Threads

  1. Double-clicking .jar files?
    By StokedOnMe in forum New To Java
    Replies: 11
    Last Post: 04-23-2014, 01:52 PM
  2. right clicking JTable row
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 06-28-2010, 12:41 AM
  3. Clicking on an oval in JPanel
    By chielt in forum Java 2D
    Replies: 6
    Last Post: 03-17-2010, 01:08 AM
  4. Auto Clicking Application
    By dpedroia15 in forum New To Java
    Replies: 6
    Last Post: 03-02-2010, 10:35 PM
  5. Java3D: Clicking and getting coordinates?
    By seabhcan in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2008, 03:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •