CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 1999
    Posts
    3

    .requestFocus() does not seem to work?



    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.



  2. #2
    Join Date
    May 1999
    Posts
    3

    Re: .requestFocus() does not seem to work?

    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




  3. #3
    Guest

    Re: .requestFocus() does not seem to work?

    I find that using this method will set the focus in the JTextfield but the cursor will not blink initially.


  4. #4
    Join Date
    May 1999
    Location
    USA
    Posts
    27

    Re: .requestFocus() does not seem to work?

    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


  5. #5
    Join Date
    May 1999
    Posts
    2

    Re: .requestFocus() does not seem to work?

    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!



  6. #6
    Join Date
    May 1999
    Location
    TamilNadu, India
    Posts
    42

    Re: .requestFocus() does not seem to work?

    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


  7. #7
    Join Date
    May 1999
    Location
    USA
    Posts
    27

    Re: .requestFocus() does not seem to work?

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured