Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setting focus to a TextField

 
Krista Meek
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call theTextField.requestFocus() .
 
Krista Meek
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic