To get the behavior of responding to a mouse over the button and enter being pressed I would:
- Use Key Bindings attached to the JButton to allow it to respond to the enter key.
- Make sure that when doing the above use the InputMap that is associated with the
JComponent.WHEN_IN_FOCUSED_WINDOW constant so that the button doesn't actually have to have focus to respond but needs to be in the focused window.
- Of course bind to the KeyStroke associated with KeyEvent.VK_ENTER.
- In the key bindings action, check if the button's model is in the rollOver state by calling
isRollOver() on the model.
- and if so, respond.
- Note that none of this above requires a MouseListener because I'm using the ButtonModel's isRollOver in place of that.
As an example, my SSCCE:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class MouseKeyResponse extends JPanel {
private JButton button = new JButton("Button");
public MouseKeyResponse() {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("button clicked");
}
});
add(button);
setUpKeyBindings(button);
}
private void setUpKeyBindings(JComponent component) {
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = component.getInputMap(condition);
ActionMap actionMap = component.getActionMap();
String enter = "enter";
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter);
actionMap.put(enter, new EnterAction());
}
private class EnterAction extends AbstractAction {
@Override
public void actionPerformed(ActionEvent evt) {
if (button.isEnabled() && button.getModel().isRollover()) {
System.out.println("Enter pressed while button rolled over");
button.doClick();
}
}
}
private static void createAndShowGui() {
MouseKeyResponse mainPanel = new MouseKeyResponse();
JFrame frame = new JFrame("MouseKeyResponse");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
"I know I should add something to the loop in the action listener": what loop? And why does this statement make me nervous? Please give the details of your problem. Assume that we have no idea what your program looks like or is doing.