Click to See Complete Forum and Search --> : .requestFocus() does not seem to work?


Reza Beheshti
January 27th, 1999, 04:26 PM
I can't get the .requestFocus() for a component to work.

I'd like to have the cursor on a particular input field when the dialog opens, so I call the Component.requestFocus() as the last thing in the dialog init method, but it doesn't work!

What am I missing?


P.S This is all Swing 1.1 stuff.


Thanks.

sanju
May 18th, 1999, 06:52 AM
Request focus will work for controls like you can give as given below

suppose let us assume that you are having a button and you have also written an event to it
in that event write the code like this



TextField t=new TextField(10);
add(t);
t.requestFocus()



create an object for text field add it to you component and give request focus
it will work and it also works for text area


my name is viji and i am working in ssi

May 18th, 1999, 08:56 PM
I find that using this method will set the focus in the JTextfield but the cursor will not blink initially.

amaresh
May 27th, 1999, 11:27 AM
Probably you are missing this line "setRequestFocusEnabled(true)" before you call "requestFocus()".
This according to the documentation, should not be necessary, as the "setRequestFocusEnabled" is defaulted to be true. But still try and see if it works.
Thanks and Regards
Amaresh

Tom Verstraete
May 31st, 1999, 06:14 AM
This seems to be a problem in Swing at the moment. I encountered the same thing: the JTextFiels has focus so you type text in it, but you don't see any cursor blinking. I don't know a solution to this. Any help would be great!

srini_raghav
May 31st, 1999, 09:05 AM
Request Focus works finely if the entire control is created fully and displayed. Suppose if u r trying to put the focus on a component in a applet or a dialog or any container... instead of writing the code in the init write the same code in the start of the container. I mean if u write this in the init of the applet it will never work.. If u try this in the start of the applet it will work finely.. Infact worked finely to me.
Suppose if u r doing the same for frame or dialog u have call the request focus after creating the dialog completely.

thanks lot,
Srini

amaresh
June 2nd, 1999, 10:15 AM
Well, there are two issues here. One is focus and the other one is cursor blinking.
I guess the default cursor is "pointer with arrow tip". You need to change the cursor to "text Cursor" if you want to see cursor blinking.
To summarize, you need to do the following things on a non-focussed text field in-order to get focus and see the cursor blinking.
1) Change the cursor to "Text Cursor". You need to do something like the following:
m_myTextField.setCursor(java.awt.Cursor.getPredefinedCursor(java.awt.Cursor.TEXT_CURSOR));
2) Call "requestFocus()"
I hope this helps.
Thanks,
Amaresh