aspose file tools*
The moose likes Beginning Java and the fly likes how do I change the focus? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do I change the focus?" Watch "how do I change the focus?" New topic
Author

how do I change the focus?

Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Oct 12, 2001
Posts: 64
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

Joined: May 17, 2001
Posts: 2823
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

Joined: Oct 12, 2001
Posts: 64
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

Joined: Jan 23, 2002
Posts: 52
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

Joined: Jul 08, 2001
Posts: 32
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

Joined: Oct 12, 2001
Posts: 64
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do I change the focus?
 
Similar Threads
hashmap
Change Image Of Mouse Cursor
Getting the Values of Dynamically Rendered TextFields
How to do a preview page for submitted form
Positioning the cursor in an input field