Results 1 to 8 of 8
  1. #1
    Iyengar is offline Member
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Default requestFocusInWindow() not working

    I am trying to get the initial focus to a Text Filed and use requestFocusInWindow. But for some reason it is not working. Any idea what could be the problem.

    Here is the part of the code.

    jcrewCodeTextFld_.requestFocusInWindow();

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Look up this method in the Component class api Method Summary section and follow the link to the Method Detail section. It contains a lot of information about how this works. You may have to place the method call after the app is made visible.

  3. #3
    Iyengar is offline Member
    Join Date
    Feb 2008
    Posts
    3
    Rep Power
    0

    Default

    Thanks. This line did the trick
    this.setInitialFocus(getMDateFld());

  4. #4
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    I am also having trouble with requestFocusInWindow().

    I am creating a JPanel which contains two JPanels.

    Let's call the first contained panel "Canvas." It is a customized control.

    I want "Canvas" to request the focus, so I call isFocusable() in the constructor.

    I'm wanting "Canvas" to get the focus so that it can respond to keyboard input.

    I call requestFocusInWindow() whenever the mouse is moved or dragged over the "Canvas," but the method always returns false.

    I checked the API docs to make sure I'm doing everything right: setVisible(true); setEnabled(true).

    I even tried creating a simplified app which does nothing but request the focus, but even it does not work. It is simply a Frame which calls setVisible(true) and setEnabled(true) and isFocusable() and then requestFocusInWindow(), but even that fails.

    Any ideas you have would be appreciated.
    -Scott

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Try calling setFocusable(true) on the "Canvas" to get it to accept keyboard input.

  6. #6
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    I was already calling setFocusable(true), but it does not accept keyboard input.

    When I call requestFocusInWindow(), it returns "false," telling me that the request was denied.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    Just a guess, but you may need to wrap the requestFocusInWindow in a SwingUtilities.invokeLater.

    To get better help faster, post a SSCCE that clearly demonstrates the problem. Link: SSCCE : Java Glossary

    db

  8. #8
    ScottVal is offline Member
    Join Date
    Jan 2009
    Posts
    35
    Rep Power
    0

    Default

    Thanks for your response.

    Although I didn't use invokeLater as you suggested, your idea jogged my mind to try something.

    I was calling requestFocusInWindow() every time the mouse was pressed, moved or dragged over the control.

    I created this control in NetBeans as a JPanel Form. I created a keyPressed event within the form.

    Here's my new feature: I'm counting the number of accepted and denied requestFocusInWindow() calls.

    It turns out the call usually is denied, but it sometimes is accepted, enough to make the keyPress event work.

    So, I'm all better now. Thanks. -Scott

Similar Threads

  1. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  2. sendredirect not working
    By ranga in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2008, 10:48 AM
  3. working with mp3 in java
    By po0oker in forum Advanced Java
    Replies: 1
    Last Post: 10-30-2007, 08:20 PM
  4. Webservice ain't working?
    By marcelman in forum Networking
    Replies: 0
    Last Post: 08-10-2007, 02:48 AM
  5. Working With ANT
    By JavaForums in forum Eclipse
    Replies: 0
    Last Post: 04-26-2007, 08:16 PM

Posting Permissions

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