17

I have a JFrame with three JButtons on it. I have set txtSearch (a JTextField component) to have the focus when JFrame loads. One of the buttons is set as the default button. This is my code:

private void formWindowOpened(java.awt.event.WindowEvent evt) 
{
     // btnRefresh.setMnemonic(KeyEvent.VK_R); // Even if this line 
                                               // is not commented, but
                                               // still the event wouldn't fire.
     this.getRootPane().setDefaultButton(btnRefresh);
}

When it loads, the button is just selected, but it did nothing when the Enter key was being pressed. How do I correctly implement it?

btnRefresh.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        btnRefreshActionPerformed(evt);
    }
});

private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                           
    JOptionPane.showMessageDialog(this, "Pressed!");
    // Other codes here (Replace by JOptionPane)
}  
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • I found that the easiest way is to call requestFocusInWindow() AFTER the JFrame is visible to set a default button. Hope this helps someone. – GeriBoss Jul 11 '14 at 11:23

2 Answers2

10

What component has focus when the JFrame comes up? I ask because some components "eat" the Enter key event. For example, a JEditorPane will do that.

Also, when you assign an ActionListener to JTextField, the ActionListener will be called instead of the DefaultButton for the root pane. You must choose either to have an ActionListener or a DefaultButton, but you can't have both fire for the same JTextField. I'm sure this applies to other components as well.

Chuck Krutsinger
  • 2,830
  • 4
  • 28
  • 50
  • I have set `JTextField` to be focused when `JFrame` loads. – John Woo Mar 08 '12 at 02:45
  • 1
    correct analysis, not a solution :-) @johntotetwoo from your accepting this I assume it pointed it into the direction of a solution - you might consider to edit your question with that solution – kleopatra Mar 08 '12 at 14:58
  • Brilliant.. I have an actionListener on my password field and had failed to figure out that it was the cause of the defaultbutton not responding. Thanx – Zuko Feb 12 '16 at 04:35
0

I don't see what you are doing incorrectly from what is posted. Here is a short example that works. Perhaps it will reveal something useful to you.

import java.awt.BorderLayout; 
public class ExampleFrame extends JFrame 
{
  private JPanel    m_contentPane;
  private JTextField m_textField;

  /**
   * Launch the application.
   */
  public static void main(String[] args)
  {
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                ExampleFrame frame = new ExampleFrame();
                frame.setVisible(true);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });
  }

  /**
  * Create the frame.
   */
  public ExampleFrame()
  {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    m_contentPane = new JPanel();
    m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    m_contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(m_contentPane);

    m_textField = new JTextField();
    m_contentPane.add(m_textField, BorderLayout.NORTH);
    m_textField.setColumns(10);

    JButton btnNewButton = new JButton("Default");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(ExampleFrame.this, "Default.");
        }
    });
    m_contentPane.add(btnNewButton, BorderLayout.CENTER);

    JButton btnNewButton_1 = new JButton("Not default");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            JOptionPane.showMessageDialog(ExampleFrame.this, "Not default.");
        }
    });
    m_contentPane.add(btnNewButton_1, BorderLayout.WEST);
    m_textField.requestFocus();
    getRootPane().setDefaultButton(btnNewButton);
  }
}
Chuck Krutsinger
  • 2,830
  • 4
  • 28
  • 50
  • 1
    add an actionListener to the textField and see what happens :-) – kleopatra Mar 08 '12 at 17:13
  • You nailed it kleopatra! When I put in the ActionListener, the default button failed to fire. johntotetwoo, you need to *remove* the action listener to have the default button fire. – Chuck Krutsinger Mar 08 '12 at 17:33