Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: addActionListeners() not recognized ENTER key event for a JTextField

  1. #1
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question addActionListeners() not recognized ENTER key event for a JTextField

    Hello, all. I have the following code in my init() method:

    		friend = new JTextField(TEXT_FIELD_SIZE);
    		friend.setActionCommand("Add Friend");
    		add(friend, WEST);
    		add(new JButton("Add Friend"), WEST);
     
    		canvas = new FacePamphletCanvas();
    		add(canvas);
     
    		addActionListeners();

    and the following code in my actionPerformed submethod:

        public void actionPerformed(ActionEvent e) {
        	String cmd = e.getActionCommand();
        	if(cmd.equals("Add")) addProfile();
        	if(cmd.equals("Delete")) deleteProfile();
        	if(cmd.equals("Lookup")) lookupProfile();
     
        	if(cmd.equals("Change Status")) changeStatus();
        	if(cmd.equals("Change Picture")) changePicture();
        	if(cmd.equals("Add Friend")) addFriend();
    	}

    I'm using Eclipse as my compiler. I used the debugging tools to discover that when I hit the ENTER key, my program is not recognizing the ActionEvent. It does recognize the ActionEvent when I hit the JButton with the same ActionCommand. Any suggestions? Thank you!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Assuming you've added the ActionListener to the JTextField, the JTextField must have focus to fire the event. Focus occurs when you type inside, or by calling requestFocus

  3. #3
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Quote Originally Posted by copeg View Post
    Focus occurs when you type inside, or by calling requestFocus
    Thanks, copeg. But when I run the program, I do click in the JTextField, type a string, and then hit ENTER. Nothing happens. When I hit the JButton that has the same CommandAction, then the event fires. Any other ideas why my program isn't recognizing the ENTER key as a CommandAction event?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Post an SSCCE that demonstrates the behavior. And make sure you have added the ActionListener to the JTextField.

  5. #5
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Pardon my newbie-ness... what's an SSCCE? Also, is there a different way to add the ActionListener to the JTextField specifically? I thought I had already added it properly as demonstrated in the 1st set of code above. Thank you so much for your continued help.

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Quote Originally Posted by wltrallen2 View Post
    Pardon my newbie-ness... what's an SSCCE?
    Short, Self Contained, Correct Example - see the link in my signature, or just google the acronym.

    Quote Originally Posted by wltrallen2 View Post
    I thought I had already added it properly as demonstrated in the 1st set of code above. Thank you so much for your continued help.
    You call a method addActionListeners(), and did not post the code. We can only guess what you did...hence the request for an SSCCE

  7. #7
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    I hope this is an SSCCE. I think you should be able to run this and recreate my results. Thanx again for any help.

    import acm.program.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class TestActionListeners extends ConsoleProgram {
     
    	public void init() {
    		JTextField status = new JTextField(25);
    		status.setActionCommand("Change Status");
    		add(status, WEST);
    		add(new JButton("Change Status"), WEST);
     
    		addActionListeners();
    	}
     
    	public void actionPerformed(ActionEvent e) {
    		String cmd = e.getActionCommand();
    		if(cmd.equals("Change Status")) println("Status changed.");
    	}
     
    }

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Not an SSCCE....where is the addActionListeners method defined? Are you required to use the class ConsoleProgram (it is a 3rd party library, and IMO for beginners it defeats the process of learning the ins and outs of how to create a GUI)? And where in that method could it add an ActionListener to the local variable status? Recommend reading the following? How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Last edited by copeg; May 28th, 2012 at 10:42 AM.

  9. #9
    Junior Member
    Join Date
    May 2012
    Location
    California
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: addActionListeners() not recognized ENTER key event for a JTextField

    Thanks, copeg. I will check out the reading and go from there.

Similar Threads

  1. WSDL Classes not recognized
    By jb1111 in forum Web Frameworks
    Replies: 0
    Last Post: March 20th, 2012, 09:37 PM
  2. IP Can't Recognized
    By Voipswitch1 in forum Java Networking
    Replies: 1
    Last Post: February 8th, 2012, 06:03 PM
  3. Enter in name ....need a little bit of help!
    By LAerving in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2012, 09:14 PM
  4. Why does it enter this if ?
    By piulitza in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 25th, 2011, 11:42 AM
  5. Methods in java class files are not recognized.
    By Girish in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2010, 06:44 AM