Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

KeyBinding issue with JTextField  RSS feed

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The textField is returning an empty string "". Can't find a solution as to why, I think that it is the key binding. Haven't done a SSCCE yet.




 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The action command is not the text of the text field. It is the character that was typed. Don't use the action command.

Instead you need to get the text directly from the text field:



Also, there is no need to manually create the key bindings. JTextField has a special method to handle the Enter key. Just use:

 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wasn't aware that JTextField already supported the enter key. Thank You....
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:

The action command is not the text of the text field. It is the character that was typed. Don't use the action command.

Instead you need to get the text directly from the text field:




getActionCommand() gives you a String representing the action command. The value is component specific; for a JButton you have the option to set the value with setActionCommand(String command) but for a JTextField if you don't set this, it will automatically give you the value of the text field. According to the javadoc this is for compatability with java.awt.TextField.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comment from the Documentation applies when you are using an ActionListener. When you use Key Bindings, you get the character typed.

Using the suggestion I made which is to get the text field from the event and then get the text from the text field will work whether you use an ActionListener or use Key Bindings or the Enter key.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:The comment from the Documentation applies when you are using an ActionListener. When you use Key Bindings, you get the character typed.

Using the suggestion I made which is to get the text field from the event and then get the text from the text field will work whether you use an ActionListener or use Key Bindings or the Enter key.


Thank You Rob....

I deleted the key binding because I was unaware that JTextField had support for the enter key built into the API. With that being said wouldn't it be more beneficial to use getActionCommand() over getSource() within the action since getSource() returns an instance of the component and getActionCommand() in my case returns the value of the text field. Really from what I know it is just one less line of code.
 
Rob Camick
Ranch Hand
Posts: 2753
11
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really from what I know it is just one less line of code.


One more line of code is nothing. I would not use that do make my decision for this or any other piece of coding.

For me, it depends what you find clearer to understand and more self documenting.

Using the getActionCommand() method implies people need to know how the value of the action command is determined. It is not documented well in the API. That is if you look at the ActionEvent.getActionCommand() method it is not documented. If you look at the JTextField.setActionCommand() it is not document what the value is if the method is not invoked. You need to read the JTestField general API introduction to find out how the value is determined.

It is very common in a listener to get the source of the event and then access a property of that source. I find the two lines of code very self documenting what the code is doing.

Having said all that I must admit when using a JButton I do sometimes use the getActionCommand() method since that concept has always been well explained with examples in the Swing tutorial on How to Use Buttons. But even the tutorial explicitly sets the action command so the code is more self documenting. On the other hand I was not aware that when using a text field the getActionCommand() method would return the text. Even the Swing tutorial on How to Use Text Fields uses the getText() method.

So either way is valid. I can't say what the best approach is.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!