swing - How to set a Single key Mnemonic in button in Java? -
i working on project , want set mnemonic on buttons. problem mnemonic works on pairing key example (alt+f) etc. want on single key.
have @ keybindings,
then can attach key jbutton
here 1 example code help, press c on 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 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, saw me."); } } public static void main(string... args) { swingutilities.invokelater(new runnable() { public void run() { new buttonexample().displaygui(); } }); } }
Comments
Post a Comment