Results 1 to 10 of 10
  1. #1
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default JTextField & JTextArea won't receive focus in a modal JDialog window

    Hi,

    I have put some components including a JTextField and JTextArea in a sub-class of JDialog.

    The problem is that the text field and text area components won't receive focus although they are set to be focusable, enabled and editable. Setting focus explicity through component.requestFocusInWindow() won't work either.

    I am not able to replicate this problem through a small test program; so, can't produce the sample code here.

    Please suggest what could be the reason(s) for this unexpected behavior? I am willing to provide any additional information that you may require to assist.

    Regards,
    Dinesh

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by javaexplorer View Post
    I am not able to replicate this problem through a small test program; so, can't produce the sample code here.

    Please suggest what could be the reason(s) for this unexpected behavior?
    Sorry, but without an SSCCE, I have no idea.

    I am willing to provide any additional information that you may require to assist.
    Keep trying to create the SSCCE.

  3. #3
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > I am not able to replicate this problem through a small test program

    is the format the same?
    e.g. some component (e.g. JButton) added first,
    then added next, a textComponent you're trying to get focus to

    > Setting focus explicity through component.requestFocusInWindow()

    requestFocusInWindow only works if the component is visible,
    so, for a modal dialog, it would need to be placed 'before' setVisible(true)
    and wrapped in a SwingUtilities.invokeLater, or in a timer, so the call
    is delayed until after the dialog is visible

  4. #4
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    Hi curmudgeon,
    Thanks for letting me know that you are not able to assist.

    Hi Michael,

    Thanks for your response.

    is the format the same?
    The format is same as the components were added in the same order as they were added onto the original JDialog.

    requestFocusInWindow only works if the component is visible,
    so, for a modal dialog, it would need to be placed 'before' setVisible(true)
    and wrapped in a SwingUtilities.invokeLater, or in a timer, so the call
    is delayed until after the dialog is visible
    I am invoking requestFocusInWindow on a button click in JDialog; as the dialog is visible when this method is invoked and also since the call is in the event dispatch thread itself, there doesn't appear to be any need to invoke the SwingUtilities.InvokeLater method.

    I added a couple of other editable components like JSPinner, JPasswordField, but they all won't receive focus. The only components that are able to receive focus are JButton and JComboBox.

    With no resolution to the issue in sight, I changed the dialog to a modeless one, and the issue was instantly resolved. Though a modeless dialog is not exactly what I need, I think I will have to live with it.

    Regards,
    Dinesh

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Are there multiple modal dialogs opened simultaneously?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    not able to replicate this problem through a small test program
    That should give you a clue. What is different between your small test program and the full program?

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by javaexplorer View Post
    Hi curmudgeon,
    Thanks for letting me know that you are not able to assist.
    You're quite welcome. Looks like without your being able to create and post a small program that reproduces your problem, I'm in good company.

  8. #8
    Michael Dunn is offline Member
    Join Date
    Jul 2008
    Posts
    62
    Rep Power
    0

    Default

    > With no resolution to the issue in sight, I changed the dialog to a modeless one, and the issue was instantly resolved.

    would indicate your actionListener code is added 'after'
    the line where the dialog is set to visible i.e. not added until the dialog closes

    check by adding a System.out.println() in the actionPerformed

  9. #9
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    Thanks to all of you for your inputs.

    Are there multiple modal dialogs opened simultaneously?
    No, there is only a single modal dialog opened.

    That should give you a clue. What is different between your small test program and the full program?
    The only difference that I see is that in the real program, the Modal dialog is launched from a JInternalFrame, while in the test program it is launched from a JFrame.

    would indicate your actionListener code is added 'after'
    the line where the dialog is set to visible i.e. not added until the dialog closes
    Let me clarify that my concern is not about setting an initial focus component. The components won't receive focus even when the dialog is opened and they are clicked. The focus won't be set programmatically as well although the call to component.requestFocusInWindow succeeds.

  10. #10
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Lightbulb

    you can just make the components be focused with

    SwingUtilities.invokeLater(new Runnable() { public void run() { yourComponent.requestFocus(); } } );

    Or maybe the problem is deeper ? :confused:
    If my answer helped you. Please click my "REP" button and add a comment
    Have a Good Java Coding :)

Similar Threads

  1. Window Focus
    By collin389 in forum New To Java
    Replies: 4
    Last Post: 12-29-2009, 03:03 AM
  2. switch focus to another window
    By tuansoibk in forum AWT / Swing
    Replies: 1
    Last Post: 11-15-2009, 05:22 AM
  3. how to let the shell window in SWT lose focus
    By kents in forum SWT / JFace
    Replies: 2
    Last Post: 06-25-2009, 10:34 AM
  4. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  5. Can't edit JTextField after modal dialog
    By JavaNerd in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 09:11 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
  •