Java/SWING Tip - Assigning an accelerator key to a JButton
We haven't been blogging much lately and I have made a decision to post useful tips and tricks I come across that could help someone else out. Many is the time that I need to implement a new feature and my first port of call is the internet.
I would love to leave feedback on many programming sites and blogs, but the fact that most require you to create an account puts me off - I don't have time at work to create a new account, confirm, navigate to the post in question, leave a comment saying "Great tip, thanks!" and unregister from the site. So, I will be reproducing tips on our blog. If this infringes copyright of some sort, let me know and I will remove the tip. I will of course give credit to the original poster.
So tip 1:
Java/SWING - Assigning an accelerator key to a JButton
Original author: Remus Stratulat (2004-08-18)
http://www.stratulat.com/technical/java/j1/
I modified Remus' original clip of code to hook up an exit action to several cancel buttons in my application. I can then deal with any custom close actions in one place:
// Setting the cancelButton to receive action when ESC is pressed
InputMap keyMap = new ComponentInputMap(cancelButton);
keyMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ExitDialogAction");
ActionMap actionMap = new ActionMapUIResource();
actionMap.put("ExitDialogAction", new ExitDialogAction(this));
SwingUtilities.replaceUIActionMap(cancelButton, actionMap);
SwingUtilities.replaceUIInputMap(cancelButton, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap);
I would love to leave feedback on many programming sites and blogs, but the fact that most require you to create an account puts me off - I don't have time at work to create a new account, confirm, navigate to the post in question, leave a comment saying "Great tip, thanks!" and unregister from the site. So, I will be reproducing tips on our blog. If this infringes copyright of some sort, let me know and I will remove the tip. I will of course give credit to the original poster.
So tip 1:
Java/SWING - Assigning an accelerator key to a JButton
Original author: Remus Stratulat (2004-08-18)
http://www.stratulat.com/technical/java/j1/
I modified Remus' original clip of code to hook up an exit action to several cancel buttons in my application. I can then deal with any custom close actions in one place:
// Setting the cancelButton to receive action when ESC is pressed
InputMap keyMap = new ComponentInputMap(cancelButton);
keyMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "ExitDialogAction");
ActionMap actionMap = new ActionMapUIResource();
actionMap.put("ExitDialogAction", new ExitDialogAction(this));
SwingUtilities.replaceUIActionMap(cancelButton, actionMap);
SwingUtilities.replaceUIInputMap(cancelButton, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap);
0 Comments:
Post a Comment
<< Home