addActionListeners() not recognized ENTER key event for a JTextField
Hello, all. I have the following code in my init() method:
Code :
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:
Code :
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!
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
Re: addActionListeners() not recognized ENTER key event for a JTextField
Quote:
Originally Posted by
copeg
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?
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.
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.
Re: addActionListeners() not recognized ENTER key event for a JTextField
Quote:
Originally Posted by
wltrallen2
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
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
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. :)
Code :
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.");
}
}
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)
Re: addActionListeners() not recognized ENTER key event for a JTextField
Thanks, copeg. I will check out the reading and go from there. :)