This discussion is archived
6 Replies Latest reply: Mar 11, 2013 4:10 PM by Maxideon RSS

How to set the focus for a JButton

UdayaShankaraGandhi Newbie
Currently Being Moderated
Hi All,

I need to set the focus for a JButton placed inside a panel which is holded by a JDialog. I have tried by setting requestFocusInWindow() and requestFocus() to the button, but still my button is not getting the focus. The below is my code:
public class ButtonFocusTest
{
   public static void main(String[] args)
   {
      JPanel myPanel = new JPanel();
      myPanel.setLayout(new FlowLayout());
     
      JTextField field = new JTextField(20);
     
      JButton focusButton = new JButton("Need Focus");

      myPanel.add(field);
      myPanel.add(focusButton);
      
      //focusButton.requestFocus();
      focusButton.requestFocusInWindow();

      JDialog dialog = new JDialog();
      dialog.setContentPane(myPanel);
      dialog.setSize(200,200);
      dialog.setVisible(true);
   }
}
Please let me know how to set the focus for the JButton in this scenario.

Thanks,
Uday
  • 1. Re: How to set the focus for a JButton
    Maxideon Explorer
    Currently Being Moderated
    Request focus after the dialog is displayable?
  • 2. Re: How to set the focus for a JButton
    jim_b_o Newbie
    Currently Being Moderated
    Use a FocusTraversalPolicy to define the default component:
    public class ButtonFocusTest {
         public static void main(String[] args) {
              JPanel myPanel = new JPanel();
              myPanel.setLayout(new FlowLayout());
    
              JTextField field = new JTextField(20);
    
              final JButton focusButton = new JButton("Need Focus");
    
              myPanel.add(field);
              myPanel.add(focusButton);
              
              // ----- Define FocusTraversalPolicy
              FocusTraversalPolicy policy = new DefaultFocusTraversalPolicy() {
    
                   @Override
                   public Component getDefaultComponent(Container aContainer) {
                        return focusButton;
                   }
                   
              };
    
              JDialog dialog = new JDialog();
              dialog.setContentPane(myPanel);
              // ----- Set FocusTraversalPolicy
              dialog.setFocusTraversalPolicy(policy);
              dialog.setSize(200, 200);
              dialog.setVisible(true);
         }
    }
  • 3. Re: How to set the focus for a JButton
    jduprez Pro
    Currently Being Moderated
    Udaya Shankara Gandhi wrote:
    I need to set the focus for a JButton placed inside a panel which is holded by a JDialog.
    Why?
    If this is to reproduce the functionality of a "default button" (the one that gets the "Enter" key stroke if you press Enter while the dialog is on top), you can use the dedicated "default button" API: from the Swing tutorial (http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#jbutton):

    The default button typically has a highlighted appearance and acts clicked whenever the top-level container has the keyboard focus and the user presses the Return or Enter key (...)
    You set the default button by invoking the setDefaultButton method on a top-level container's root pane. Here is the code that sets up the default button for the ListDialog example:

    +
    //In the constructor for a JDialog subclass:
    getRootPane().setDefaultButton(setButton);
    +
  • 4. Re: How to set the focus for a JButton
    Kleopatra Journeyer
    Currently Being Moderated
    jduprez wrote:
    Why?
    If this is to reproduce the functionality of a "default button" (the one that gets the "Enter" key stroke if you press Enter while the dialog is on top), you can use the dedicated "default button" API: from the Swing tutorial (http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#jbutton):
    fully agree with the why :-)

    Just the usual caveat: the exact functionality of the default button needs further thoughts if the enter is used by any of the contained components, f.i. a textField with a Action/Listener.

    Cheers
    Jeanette
  • 5. Re: How to set the focus for a JButton
    UdayaShankaraGandhi Newbie
    Currently Being Moderated
    jduprez wrote:
    Udaya Shankara Gandhi wrote:
    I need to set the focus for a JButton placed inside a panel which is holded by a JDialog.
    Why?
    If this is to reproduce the functionality of a "default button" (the one that gets the "Enter" key stroke if you press Enter while the dialog is on top), you can use the dedicated "default button" API: from the Swing tutorial (http://docs.oracle.com/javase/tutorial/uiswing/components/button.html#jbutton):

    The default button typically has a highlighted appearance and acts clicked whenever the top-level container has the keyboard focus and the user presses the Return or Enter key (...)
    You set the default button by invoking the setDefaultButton method on a top-level container's root pane. Here is the code that sets up the default button for the ListDialog example:

    +
    //In the constructor for a JDialog subclass:
    getRootPane().setDefaultButton(setButton);
    +
    Here in my example, i am not setting JFrame as my parent container to hold. So i am not able to use getRootPane().setDefaultButton(setButton).

    However, jimbo answer helped me in solving this. and i found another approach by adding windowFocusListener to my dilaog and setting the default button whenever windowGainedFocus() got called.
  • 6. Re: How to set the focus for a JButton
    Maxideon Explorer
    Currently Being Moderated
    That moves the request for the focus to after the dialog is displayable. That's what I was talking about in my response. In your example, if you move the requestFocusInWindow() to after the setVisible() command, it would work. And actually, it doesn't even need to be visible, just displayable. So requesting focus anytime after the dialog's addNotify() is implicitly called should work (I think).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points