aspose file tools*
The moose likes Applets and the fly likes setting focus to a TextField Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "setting focus to a TextField" Watch "setting focus to a TextField" New topic
Author

setting focus to a TextField

Krista Meek
Greenhorn

Joined: Oct 24, 2005
Posts: 12
Hi everyone,

I have a question regarding the chat applet program that I am writing.
When the applet runs, it sets focus to the TextArea as opposed to the TextField, where the user is to type. I am wondering how I go about setting the focus to the TextField, so that the user won't have to click there before typing.

I am sure it's just a simple snippet of code that I just can't figure out. Any suggestions would be appreciated.

Thanks.
K.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Call theTextField.requestFocus() .


[Jess in Action][AskingGoodQuestions]
Krista Meek
Greenhorn

Joined: Oct 24, 2005
Posts: 12
I tried that, but it still gave focus to the TextArea.

Here's the code...do I have it in the wrong place?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I don't believe a component can ask for the focus on if it hasn't been added to the GUI yet. You'll want to move that after the "add()" call, and actually I'm not sure but you may need to move it from init() to start().
Krista Meek
Greenhorn

Joined: Oct 24, 2005
Posts: 12
I placed it after the add() method and it took the focus off of the TextArea, but the cursor was not in the TextField until I clicked there.

I then commented it out in the init() and placed it in the start() method. With that, it brought the focus back on the TextArea.

So, I placed it back in the init() method, but the focus remains on the TextArea, even though this placement worked the first time, or at least took the cursor out of the TextArea.

I'm confused.
Krista Meek
Greenhorn

Joined: Oct 24, 2005
Posts: 12
OK. I started the applet up today, and it is doing exactly what I want it to do.
Does Java just suddenly work sometimes?

Oh, and thanks for the help!
[ December 09, 2005: Message edited by: Krista Meek ]
graham king
Ranch Hand

Joined: Dec 30, 2004
Posts: 133
It mighta been a browser caching issue. With applets you gotta restart your browser to see your changes each time. Otherwise you'll go nuts.


write, revise, re-write, revise, again...<br />write, revise, re-write, revise, again...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting focus to a TextField