Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do I change the focus?

 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing an applet that gets input from the user in textfields, and I want the cursor to go to the next textfield when the user hits enter. How can I do this? Someone said to call requestFocus() on the object where I want the cursor to go, and I tried this, but got compilation errors. Do I have to register FocusListener for all the textfields?
This may be really obvious to others, but I am genuinely confused about what I should be doing.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why you would use enter key to go between components. Is this required for a class assignment? Tab key is generally what is used in most apps.
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is for a class assignment. (actually I am making up my incomplete from last semester, but my teacher isn't around, so I can't even ask him questions about it.) I wasn't sure how to get the user input from the textfield (which is used later in calculations in the program) so I followed the example in our book, which used getText() to get the user input, and they used "enter" in that program.
I have looked through a couple of books as well as the API documents, and I couldn't find an example like the one I have to do, using the tab key. I thought it looked weird, since I know that applets usually use tab. But I don't know what to do.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tab key is generally used for navigation. If you don't have to use enter don't. The enter key is many times with buttons. You can get the values from the fields in a buttons action event.
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean that when the user presses the compute, I can associate a ButtonListener with the compute button and get the input from the textfields that way?
 
tormod eriksen
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that it's the call to requestFocus() that generates the compile errors?
As far as i know you don't have to register any listeners to any of the textfields to get requestFocus to work.
You can associate an ActionListener (<buttonidentifier>.addActionListener) with the compute button, and write processing code in the linked actionPerformed method.
 
Lucas Richardson
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pressing enter and having the focus move to another field isn't all that uncommon. An example is a program requiring a login, where you put in your user name, hit enter, and it automatically moves the the password field.
 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was definitely the call to requestFocus() that generated the compile errors. I am not sure why; I may have made an error with the syntax.
Yes, what I am trying to do here is very similar to the situation where the user enters their name, then hits enter, and then the focus goes to the password field. That is the kind of thing I am trying to set up.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic