java - Is there a way to override native accelerator keys? -


i have jlist filled entries native copy method can't handle. have method copies , formats selected data in way want , want method called when ctrl + c used unfortunately jlist listing accelerator keys when set them copy function in jmenu doesn't work unless give jmeny focus. there anyway can make ctrl + c use copy method instead of native 1 other having click on jmenu every time want copy something?

start taking @ how use key bindings , how use actions

start creating action describes , how does. in case, may need supply jlist want copy (i'd have kind of interface provided "copy" method of kind, that's me)

public class copyaction extends abstractaction {      private jlist list;      public copyaction(jlist list) {         putvalue(name, "copy");         putvalue(accelerator_key, keystroke.getkeystroke(keyevent.vk_c, keyevent.ctrl_down_mask));         putvalue(mnemonic_key, keyevent.vk_c);         putvalue(selected_key, "copy stuff");     }      @override     public void actionperformed(actionevent e) {         system.out.println("all copies belong us");     }      public jlist getlist() {         return list;     }  } 

now, add key binding jlist, along action...

public class testpane extends jpanel {      private jlist listofstuff;      public testpane() {         setlayout(new borderlayout());         listofstuff = new jlist();         add(new jscrollpane(listofstuff));          copyaction copyaction = new copyaction(listofstuff);         listofstuff.getactionmap().put("copy", copyaction);     }      public jlist getlistofstuff() {         return listofstuff;     }  } 

now, can use action buttons well...

testpane tp = new testpane(); copyaction copyaction = new copyaction(tp.getlistofstuff());  jmenubar mb = new jmenubar(); jmenu menu = new jmenu("edit"); mb.add(menu); menu.add(copyaction);  jframe frame = new jframe("testing"); frame.setjmenubar(mb); 

and runnable example....

import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.keyevent; import javax.swing.abstractaction; import javax.swing.jframe; import javax.swing.jlist; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.keystroke; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception;  public class test {      public static void main(string[] args) {         new test();     }      public class copyaction extends abstractaction {          private jlist list;          public copyaction(jlist list) {             putvalue(name, "copy");             putvalue(accelerator_key, keystroke.getkeystroke(keyevent.vk_c, keyevent.ctrl_down_mask));             putvalue(mnemonic_key, keyevent.vk_c);             putvalue(selected_key, "copy stuff");         }          @override         public void actionperformed(actionevent e) {             system.out.println("all copies belong us");         }          public jlist getlist() {             return list;         }      }      public test() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  testpane tp = new testpane();                 copyaction copyaction = new copyaction(tp.getlistofstuff());                  jmenubar mb = new jmenubar();                 jmenu menu = new jmenu("edit");                 mb.add(menu);                 menu.add(copyaction);                  jframe frame = new jframe("testing");                 frame.setjmenubar(mb);                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private jlist listofstuff;          public testpane() {             setlayout(new borderlayout());             listofstuff = new jlist();             add(new jscrollpane(listofstuff));              copyaction copyaction = new copyaction(listofstuff);             listofstuff.getactionmap().put("copy", copyaction);         }          public jlist getlistofstuff() {             return listofstuff;         }      }  } 

Comments