Default button in JFrame is not firing when the enter key is being pressed

扶醉桌前 提交于 2020-01-02 00:57:14

问题


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)
}  

回答1:


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.




回答2:


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);
  }
}


来源:https://stackoverflow.com/questions/9612020/default-button-in-jframe-is-not-firing-when-the-enter-key-is-being-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!