Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

placing the focus in a textbox when the application starts

 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may seem like a lame question, but I've looked all over, up and down in java docs looking for the right method or something to set the focus in a text box. Right now, I have one text box in an application and want to set the focus there when the application starts. hope someone can help.
Clyde
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a TextField object named field the following will set the caret to the first position in the field and set the focus to that field:
field.setCaretPosition(0);
field.requestFocus();
I found that if you tabbed or jumped around fields and then came back sometimes the caret wouldn't show up in the same spot. I didn't think that looked good so I added the call to setCaretPosition.
Hope this helps.
John
 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
Thanks for your help. Thought things would work right off the bat, especially after the file compiled. However, when I tried to run the program, I got an "Exception in thread "main" java.awt.IllegalComponentStateException: Cannot set caret position until after the peer has been created." message. What is the peer and how do I create it? I've never heard of it. Can't find it in my book nor in the online help(I guess it's there somewhere, but so far I haven't found it).
Clyde
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm...
I have never heard of that exception, so I'll have to look it up.
The exception you are seeing occurs when the component is not in an appropriate state (makes sense...) so maybe you are trying to set the position before you have added the field or area to the container? You also may need to make the app visible first, then set the position. Unfortunately my code where I do this is at home so I can't just look at it. It may help to post your code (if it is fairly short)along with the error messages you are receiving.
Remember! Just because something compiles doesn't mean that it will run. Or it may run but not do things that you expect (or want). Having something compile is just the start of the fun! :-)
BTW, are you using the AWT or Swing?
John
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clyde,
Had a chance to do a little coding this morning. You have to set the caret (and focus) after you add the TextField to the container and make the container visible. After that no exception.
The peer that is being refered to may be something in the java.awt.peer package. Take a look at the API documentation for more info on this package.
John
 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
Thanks for the tips. I will try them in a few days. Gotta finish some java assignments and study for the final.
Clyde
 
eric moon
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I had a similar problem trying to get focus to a new window within an app. It was a bug in 1.2.2, which may or may not have been fixed. Here's the workaround I was given, overriding the paint() method of the frame. There was some concern about doing this, but it worked for me:

/*fix for bug in 1.2 that kept new frame from getting focus.
May be fixed by now...... */
public void paint(Graphics g) {
super.paint( g );
while(!this.isVisible()) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {}
}
frame.focusWorthyComponent.requestFocus();
}
 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
Thank you so much for your suggestion. After a few trys, I was able to set the focus in a text field that was below a text area (which normlly got the focus on start up).
Thanks a bunch,
Clyde
 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric,
Thanks for your suggestion. Never got to it because the previous suggestion worked for me, but I will keep you suggestion in mind for opening new windows.
Thanks Again,
Clyde
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clyde,
Glad you got it to work.
How did the final go?
John
 
clyde jones
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
I think it went OK. Will have to wait a few weeks before I get my grade. On to advanced java!!!
Clyde
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic