• Post Reply Bookmark Topic Watch Topic
  • New Topic

unexpected cursor behavior in a JTextField  RSS feed

 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a keyboard using Java swing. I am using the actions performed in response to clicking on a JButton to manipulate text in a Java TextField.

I initialize an empty JTextField and click on it.
Then I click on the JButton.
I expect the character "x" to be added to the JTextField and I expect the cursor to move to the right of the character.
The character is added as expected, but the cursor doesn't move!

Here is a very simple example of the problem:


Here is the program output:

------------------------------
text= ""
cursor is located at position 0

text= "x"
cursor is located at position 0
------------------------------

thanks in advance,
Joe
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I expect the character "x" to be added to the JTextField and I expect the cursor to move to the right of the character.


Your code is way too complicated. It is the default behaviour to position the caret after the text.

All you need in your ActionListener is:


 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Your solution works but the cursor didn't appear until I replaced textField.requestFocusInWindow () with textField.grabFocus() . I didn't realize that textField.setText () automatically places the cursor at the end of the text.

I have to be able to locate the cursor independently in order to implement, for example, arrow keys and text insertion. I've found that textField.setCaretPosition does what I want so I'm good for now.

Still, I don't understand why the original example I posted resulted in moving the cursor from position 1 to position 0???

Thanks again.
Regards,
Joe
requestFocusInWindow ()
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Nikki wrote:I have to be able to locate the cursor independently in order to implement, for example, arrow keys and text insertion.


I suppose that's true, although JTextField already implements consistent handling for arrow keys and text insertion when it's focused and receiving keyboard events. So it seems to me that if you want to handle that sort of thing yourself, duplicating what the component naturally does already, you will have to make sure the JTextField doesn't get the focus.
 
Rob Camick
Ranch Hand
Posts: 2801
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you should not use grabFocus() you should be using requestFocusInWindow().

The caret will be at the end whether the text field has focus or not (at least in Windows).

If you don't experience this behaviour then create a proper SSCCE that demonstrates your problem instead of just posting the ActionListner.

That is create a JFrame with a JButton and JTextField so we can see the exact code you are using.
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the further replies. I will post a sscce.
In the meantime, I found this on your sscce explanation page:

Why do people miss such an opportunity? Very few things are as tempting as a link to the problem. To a seasoned forum helper, it is almost as tempting as a small bottle with the vague message 'drink me', ..or an exotic cocktail.

and I couldn't figure out whether that meant that a seasoned forum helper would or would not be tempted. (I certainly wouldn't be tempted by the unidentified small bottle, but that's just me).
Joe
 
Joe Nikki
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two issues:

1)my rewriting of functionality that already exists in Java. Your point is well taken and I will look into this.

2) needing to use grabFocus () to make the cursor appear in the textField:
I wrote a minimal sscce to illustrate my problem and, guess what? The problem disappeared! So I went back to my complete program to see the difference and studied the related Java docs. In hindsight it's pretty obvious: my JButton is a component of a JDialog while my JTextField is a component of the JFrame. Pressing the JButton takes the focus and therefore, textField.requestFocusInWindow will not transfer focus to the textField while textField.grabFocus will. I understand why the Java documentation says that client code should not use this method and I will start looking for alternatives.

Thanks for the roadmaps.
Joe
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!