I am working on a project and I want to set Mnemonic on buttons. But the problem is Mnemonic works on pairing key example (Alt+F) etc. But I want it to be on single key.
Asked
Active
Viewed 8,880 times
1 Answers
8
have look at KeyBindings,
then you can to attach any Key to the JButton
Here is one example code for your help, just Press C on the Keyboard :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Action;
public class ButtonExample
{
private JFrame frame;
private JButton button;
private void displayGUI()
{
frame = new JFrame("Button Mnemonic Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
Action buttonAction = new ButtonAction("CLICK ME"
, "This is a Click Me JButton");
button = new JButton(buttonAction);
button.getInputMap().put(KeyStroke.getKeyStroke('c'), "Click Me Button");
button.getActionMap().put("Click Me Button", buttonAction);
contentPane.add(button);
frame.setContentPane(contentPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
class ButtonAction extends AbstractAction
{
public ButtonAction(String text, String desc)
{
super(text);
putValue(SHORT_DESCRIPTION, desc);
}
@Override
public void actionPerformed(ActionEvent ae)
{
JOptionPane.showMessageDialog(frame, "BINGO, you SAW me.");
}
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new ButtonExample().displayGUI();
}
});
}
}
-
Again Pardon me for the edit, couldn't stop myself, since I can not answer now, the same thing as said by you. So added one example code. Please do have a look, if it's good enough for the guidance, else remove it, feel free as always :-) – nIcE cOw Jul 07 '12 at 07:39
-
@nIcEcOw glitch is that the key does something else then clicking the button, that's most probably not intended. Instead, use an Action to configure the button and re-use that same instance in the ActionMap – kleopatra Jul 07 '12 at 08:41
-
@kleopatra are you meaning to use JButton.doClick() – mKorbel Jul 07 '12 at 08:43
-
I'm aware that it's a bit unfair you getting the downvote for @nIcEcOw 's code - want to give any of you an incentive correct it :-) BTW, doClick in the action _is_ an option, just not optimal, IMO, mainly because you should _always_ favour Actions over ActionListener+properties to configure a button – kleopatra Jul 07 '12 at 08:53
-
@kleopatra : I just realized, that's what I thought I missed, as once told by trash god too about using Action :-), but since now I am away from my computer on my iPad, I need some time to edit the code again. I be obliged if you do this much needed favour :-) – nIcE cOw Jul 07 '12 at 08:53
-
@kleopatra : If I go through the [Java Docs](http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html), I see they had explained in the same way, as I had done. I guess I am missing something as always, that you won't reveal soon as always :-) – nIcE cOw Jul 07 '12 at 09:06
-
@nIcEcOw I don't see any relation of that tutorial link to your code - where in the ActionDemo do they use an ActionMap? ahh .. you did it, perfect! – kleopatra Jul 07 '12 at 09:14
-
@kleopatra : If I go by the way they said, in that then the OP's problem will persist, in the sense I have to again press `ALT + C` I guess, so I used KeyBinding as explained [here](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – nIcE cOw Jul 07 '12 at 09:18
-
@nIcEcOw never critisized the ActionMap usage, that was perfect right from the start :-) But the Action stored there _must_ do the _exact same_ thingy as clicking or pressing space or whatever user gesture is available to trigger the button – kleopatra Jul 07 '12 at 09:23
-
@kleopatra : Ahha, I just realized, that's the problem I am facing in my present Project. Shall I ask a new question regarding - If I assign a Key to say a `JTextField` like Press N, then this thingy must show my Account Name in that field, then I cann't press N, inside this `JTextField`, even if I wanted to provide a name that has N in it, since it always shows me, the UserName at the press of N key. How to go about this thingy ? – nIcE cOw Jul 07 '12 at 15:28
-
@nIcEcOw don't assign a single key that corresponds to a typeable char :-) – kleopatra Jul 08 '12 at 06:41