swing - Keystroke/Hot Key for JButton in Java -


initially using jmenu , establishing hot keys work using accelerator. working perfectly. want same behavior in jbutton stuck. here code wrote : please share ideas can go in right path.

import javax.swing.*;  import java.awt.event; import java.awt.event.*; import java.util.arrays;  public class showdialogbox{     jframe frame;     public static void main(string[] args){         showdialogbox db = new showdialogbox();     }      public showdialogbox(){         frame = new jframe("show message dialog");         // create action doing want         keystroke keysave = keystroke.getkeystroke(keyevent.vk_s, keyevent.ctrl_mask);          action performsave = new abstractaction("click me") {               public void actionperformed(actionevent e) {                       //do save                  system.out.println("clickme");             }         };            jbutton button = new jbutton("click me");           button.getactionmap().put("click me", performsave);         button.getinputmap(jcomponent.when_in_focused_window).put(keysave, "click me");          button.addactionlistener(new myaction());         frame.add(button);         frame.setsize(400, 400);         frame.setvisible(true);         frame.setdefaultcloseoperation(jframe.exit_on_close);     }      public class myaction implements actionlistener{         public void actionperformed(actionevent e){                string[] items = {                     "1", "2", "3"                 };                 jlist list = new jlist(items);                 jpanel panel = new jpanel();                 panel.add(list);                 joptionpane.showmessagedialog(null, panel);          } 

thanks in advance

to key bindings work, use keyevent.ctrl_down_mask not keyevent.ctrl_mask.

myself, prefer use abstractactions on actionlisteners, since way menus , buttons can share same actions, , actions can have own mnemonic key, although needs alt-key combination. example:

import java.awt.component; import java.awt.dialog.modalitytype; import java.awt.dimension; import java.awt.window; import java.awt.event.actionevent; import java.awt.event.keyevent;  import javax.swing.*;  public class showmydialogtest {     private static void createandshowgui() {       jframe frame = new jframe("show mydialog test");        jpanel mainpanel = new jpanel();       action showaction = new showdialogaction(frame, "show dialog");       final jbutton showdialogbtn = new jbutton(showaction);       mainpanel.add(showdialogbtn);       mainpanel.setpreferredsize(new dimension(600, 400));        keystroke keystroke = keystroke.getkeystroke(keyevent.vk_s, keyevent.ctrl_down_mask);       int condition = jcomponent.when_in_focused_window;       inputmap inputmap = showdialogbtn.getinputmap(condition);       actionmap actionmap = showdialogbtn.getactionmap();       inputmap.put(keystroke, keystroke.tostring());       actionmap.put(keystroke.tostring(), new abstractaction() {           @override          public void actionperformed(actionevent arg0) {             showdialogbtn.doclick();          }       });        jmenuitem showmenuitem = new jmenuitem(showaction);       jmenuitem exitmenuitem = new jmenuitem(new disposeaction("exit", keyevent.vk_x));       jmenu filemenu = new jmenu("file");        filemenu.setmnemonic(keyevent.vk_f);       filemenu.add(showmenuitem);       filemenu.add(exitmenuitem);        jmenubar menubar = new jmenubar();       menubar.add(filemenu);       frame.setjmenubar(menubar);              frame.setdefaultcloseoperation(jframe.dispose_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();          }       });    } }  class showdialogaction extends abstractaction {    private jframe frame;    private showdialogpanel dialogpanel;    private jdialog dialog;     public showdialogaction(jframe frame, string name) {       super(name);       int mnemonic = (int) name.charat(0);       putvalue(mnemonic_key, mnemonic);       this.frame = frame;    }     @override    public void actionperformed(actionevent e) {       if (dialogpanel == null) {          dialogpanel = new showdialogpanel();       }       if (dialog == null) {          dialog = new jdialog(frame, "my dialog", modalitytype.application_modal);          dialog.getcontentpane().add(dialogpanel);          dialog.pack();       }       dialog.setlocationrelativeto(frame);       dialog.setvisible(true);       // todo add code extracts data dialogpanel     } }  class showdialogpanel extends jpanel {    private jbutton button = new jbutton(new disposeaction("close", keyevent.vk_c));     public showdialogpanel() {       add(button);    } }  class disposeaction extends abstractaction {    private static final long serialversionuid = 1l;     public disposeaction(string name, int mnemonic) {       super(name);       putvalue(mnemonic_key, mnemonic);    }     @override    public void actionperformed(actionevent e) {       component component = (component) e.getsource();       window win = swingutilities.getwindowancestor(component);       if (win == null) {          jpopupmenu popup = (jpopupmenu) component.getparent();          if (popup == null) {             return;          }          component = popup.getinvoker();          win = swingutilities.getwindowancestor(component);       }       win.dispose();    } } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -