This discussion is archived
2 Replies Latest reply: Dec 5, 2012 7:05 PM by 976900 RSS

How to trigger F1 keys action when click on Jbutton using KeyBindings?

976900 Newbie
Currently Being Moderated
I really need help on to trigger F1 key action when i click on Jbutton.

I already check out like KeyBindings, mostly tutorial works for pressed key to trigger the button action example Enter key, or space key than trigger the Button action. But right now, my intention is vice versa, I want to click JButton than trigger the F1 key action.

I try the KeyBindings but having a problem on how to set code to trigger the F1 key action

Any ideas or solution on this? Here is the code:

BUTTON:

final String helpIconLocation = new String("/icons/help_new.png");
iconResource = CustomizedToolbar.class.getResource(helpIconLocation);
icon = new ImageIcon(iconResource);
actionButton = new JButton(icon);
actionButton.setFocusable(false);
actionButton.addActionListener(new JavaHelpHandler());


ACTIONLISTENER:

private class JavaHelpHandler implements ActionListener {

@Override
public void actionPerformed(ActionEvent arg0) {


JavaHelpKeyAction javaHelpKeyAction = new JavaHelpKeyAction();

KeyStroke helpKey = KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0);
InputMap inputMap = actionButton.getInputMap();
inputMap.put(helpKey, "F1");
actionMap = actionButton.getActionMap();
actionMap.put("F1", javaHelpKeyAction);

log.debug("Print inputMap :: " + inputMap);
log.debug("Print inputMap :: " + inputMap.allKeys()[0].toString());
log.debug("Print actionMap :: " + actionMap);
log.debug("Print actionMap :: " + actionMap.allKeys()[0].toString());


}

}

private class JavaHelpKeyAction implements Action {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
log.debug(">>>> Enter inside here >>>");

if (actionMap.allKeys()[0].toString().equals("release")) {
HelpSystemUtil.getInstance().enableHelpFromFocus(actionButton);

}



}

@Override
public void addPropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub

}

@Override
public Object getValue(String key) {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isEnabled() {
// TODO Auto-generated method stub
return false;
}

@Override
public void putValue(String key, Object value) {
// TODO Auto-generated method stub

}

@Override
public void removePropertyChangeListener(PropertyChangeListener listener) {
// TODO Auto-generated method stub

}

@Override
public void setEnabled(boolean b) {
// TODO Auto-generated method stub

}

}

Thanks

Regards
Aznimah
  • 1. Re: How to trigger F1 keys action when click on Jbutton using KeyBindings?
    jduprez Pro
    Currently Being Moderated
    973897 wrote:
    I really need help on to trigger F1 key action when i click on Jbutton.

    I already check out like KeyBindings, mostly tutorial works for pressed key to trigger the button action example Enter key, or space key than trigger the Button action. But right now, my intention is vice versa, I want to click JButton than trigger the F1 key action.
    Well KeyBindings is a specific mechanism to associate a javax.swing.Action to a key type (with elaborate things such as key combination, component context,etc...).
    But the real action is in a,well, javax.swing.Action .
    The good news is , you can associate an action to a JButton (and to a number of other widgets indeeds); in the case of a JButon, the action gets executed when the button is clicked.

    In your ase the idea would be to register the same Action instance in the ActionMap of the component for which you want the F1 key binding, and in the JButton.
    See the tutorial on "How to use Actions": http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
    Best regards,

    J.
  • 2. Re: How to trigger F1 keys action when click on Jbutton using KeyBindings?
    976900 Newbie
    Currently Being Moderated
    Hi, thanks for the reply and ideas. My problem solved already, i use Robot class to trigger the FI key event ;)

Legend

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