placing the focus in a textbox when the application starts
clyde jones
Ranch Hand
Joined: Dec 02, 2000
Posts: 46
posted
0
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
Joined: Apr 05, 2000
Posts: 1441
posted
0
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
The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
clyde jones
Ranch Hand
Joined: Dec 02, 2000
Posts: 46
posted
0
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
Joined: Apr 05, 2000
Posts: 1441
posted
0
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
Joined: Apr 05, 2000
Posts: 1441
posted
0
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
Joined: Dec 02, 2000
Posts: 46
posted
0
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
Joined: Nov 26, 2000
Posts: 133
posted
0
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(); }
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
clyde jones
Ranch Hand
Joined: Dec 02, 2000
Posts: 46
posted
0
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
Joined: Dec 02, 2000
Posts: 46
posted
0
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
Joined: Apr 05, 2000
Posts: 1441
posted
0
Clyde, Glad you got it to work. How did the final go? John
clyde jones
Ranch Hand
Joined: Dec 02, 2000
Posts: 46
posted
0
John, I think it went OK. Will have to wait a few weeks before I get my grade. On to advanced java!!! Clyde
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: placing the focus in a textbox when the application starts