{"id":257,"date":"2006-05-03T10:33:02","date_gmt":"2006-05-03T15:33:02","guid":{"rendered":"http:\/\/myvogonpoetry.com\/wp\/2006\/05\/03\/overriding-jcomponents-ctrl-c-action\/"},"modified":"2011-10-21T16:07:55","modified_gmt":"2011-10-21T21:07:55","slug":"overriding-jcomponents-ctrl-c-action","status":"publish","type":"post","link":"https:\/\/myvogonpoetry.com\/wp\/2006\/05\/03\/overriding-jcomponents-ctrl-c-action\/","title":{"rendered":"Overriding JComponent&#8217;s Ctrl-C Action"},"content":{"rendered":"<p>A little Java geekery here for my own reference and anyone else that needs it&#8230;.<\/p>\n<p>I needed to override the default CTrl-C (copy) behavior on a JList (though this should work on <em>any<\/em> JComponent) to copy what I wanted to the clipboard. After Googling and playing with it, I found that this works:<br \/>\n<code><br \/>\n     InputMap im = JLIST.getInputMap(JComponent.WHEN_FOCUSED);<br \/>\n      ActionMap am = JLIST.getActionMap();<br \/>\n      Action copyAction = new AbstractAction() {<br \/>\n         public void actionPerformed(ActionEvent e) {<br \/>\n            String myString = \"this goes to clipboard\";<br \/>\n            Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();<br \/>\n            cb.setContents(new StringSelection(myString ), null);<br \/>\n         }<br \/>\n      };<br \/>\n      im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), \"Copy\");<br \/>\n      am.put(\"Copy\", copyAction);<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little Java geekery here for my own reference and anyone else that needs it&#8230;. I needed to override the default CTrl-C (copy) behavior on a JList (though this should work on any JComponent) to copy what I wanted to the clipboard. After Googling and playing with it, I found that this works: InputMap im [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[97,8],"tags":[106],"class_list":["post-257","post","type-post","status-publish","format-standard","hentry","category-code","category-geek","tag-java"],"_links":{"self":[{"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/posts\/257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/comments?post=257"}],"version-history":[{"count":1,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/posts\/257\/revisions"}],"predecessor-version":[{"id":682,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/posts\/257\/revisions\/682"}],"wp:attachment":[{"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/media?parent=257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/categories?post=257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/myvogonpoetry.com\/wp\/wp-json\/wp\/v2\/tags?post=257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}