Create keyboard shortcuts for JButton in java with swing

I am using the following code to create hotkeys for a java form using swing. If I press ALT + N, ALT + R, ALT + 1, ALT + 2, the cursor moves to the correct text fields, and I enter the value in the corresponding text fields. It is working correctly. My problem: I save and exit JButtons in this form if. I press CTRL + S, then the "Save" button will be selected at the same time. If you press CTRL + X, the exit button will be selected. How to create mnemonics for JButton? How to do CTRL + S, CTRL + X using the following code?

Thanks in advance.

package hotkeys; import java.awt.event.*; import javax.swing.*; import java.net.*; public class hotkey extends JFrame { public static void main(String arg[]) { JLabel Name = new JLabel("Name"); JTextField tf1 = new JTextField(20); Name.setLabelFor(tf1); Name.setDisplayedMnemonic('N'); JLabel Regno = new JLabel("RegNO"); JTextField tf2 = new JTextField(20); Regno.setLabelFor(tf2); Regno.setDisplayedMnemonic('R'); JLabel Mark1 = new JLabel("Mark1"); JTextField tf3 = new JTextField(20); Mark1.setLabelFor(tf3); Mark1.setDisplayedMnemonic('1'); JLabel Mark2 = new JLabel("Mark2"); JTextField tf4 = new JTextField(20); Mark2.setLabelFor(tf4); Mark2.setDisplayedMnemonic('2'); JButton b1 = new JButton("Save"); JButton b2 = new JButton("eXit"); JFrame f = new JFrame(); JPanel p = new JPanel(); p.add(Name); p.add(tf1); p.add(Regno); p.add(tf2); p.add(Mark1); p.add(tf3); p.add(Mark2); p.add(tf4); p.add(b1); p.add(b2); f.add(p); f.setVisible(true); f.pack(); } } 
+4
source share
4 answers

You need to register keyBinding in the button input command. In code (repeating a subtle version of what you were told to do in previous questions :-)

 // create an Action doing what you want Action action = new AbstractAction("doSomething") { @Override public void actionPerformed(ActionEvent e) { System.out.println("triggered the action"); } }; // configure the Action with the accelerator (aka: short cut) action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control S")); // create a button, configured with the Action JButton toolBarButton = new JButton(action); // manually register the accelerator in the button component input map toolBarButton.getActionMap().put("myAction", action); toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction"); 
+18
source

Sun has a really good description of the whole key binding problem. You can find it here:

JavaSE Key Binding Tutorial

// EDIT

Edited my sample code so you can just copy + paste it and it will work. Missing points included, thanks for the feedback.

 KeyStroke keySave = KeyStroke.getKeyStroke(KeyEvent.VK_S, Event.CTRL_MASK); Action performSave = new AbstractAction("Save") { public void actionPerformed(ActionEvent e) { //do your save System.out.println("save"); } }; JButton b1 = new JButton(performSave); b1.getActionMap().put("performSave", performSave); b1.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keySave, "performSave"); KeyStroke keyExit = KeyStroke.getKeyStroke(KeyEvent.VK_Y, Event.CTRL_MASK); Action performExit = new AbstractAction("Exit") { public void actionPerformed(ActionEvent e) { //exit System.out.println("exit"); } }; JButton b2 = new JButton(performExit); b2.getActionMap().put("performExit", performExit); b2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyExit, "performExit"); 
+9
source

Just changed the code. (the inserted code in // ** )
Only 1 comment ... Ctrl-X is a shortcut for the Cut command (along with Ctrl-C and Ctrl-V). You have editable fields in the frame. Instead, I used Ctrl-Q (quit).

 import java.awt.event.*; import java.beans.PropertyChangeListener; import javax.swing.*; import javax.swing.plaf.ActionMapUIResource; import java.net.*; public class HotKeys extends JFrame { public static void main(String arg[]) { JLabel Name = new JLabel("Name"); JTextField tf1 = new JTextField(20); Name.setLabelFor(tf1); Name.setDisplayedMnemonic('N'); JLabel Regno = new JLabel("RegNO"); JTextField tf2 = new JTextField(20); Regno.setLabelFor(tf2); Regno.setDisplayedMnemonic('R'); JLabel Mark1 = new JLabel("Mark1"); JTextField tf3 = new JTextField(20); Mark1.setLabelFor(tf3); Mark1.setDisplayedMnemonic('1'); JLabel Mark2 = new JLabel("Mark2"); JTextField tf4 = new JTextField(20); Mark2.setLabelFor(tf4); Mark2.setDisplayedMnemonic('2'); JButton b1 = new JButton("Save"); JButton b2 = new JButton("eXit"); JFrame f = new JFrame(); JPanel p = new JPanel(); p.add(Name); p.add(tf1); p.add(Regno); p.add(tf2); p.add(Mark1); p.add(tf3); p.add(Mark2); p.add(tf4); p.add(b1); p.add(b2); // ***************************************************** ActionMap actionMap = new ActionMapUIResource(); actionMap.put("action_save", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Save action performed."); } }); actionMap.put("action_exit", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.out.println("Exit action performed."); } }); InputMap keyMap = new ComponentInputMap(p); keyMap.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.Event.CTRL_MASK), "action_save"); keyMap.put(KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.Event.CTRL_MASK), "action_exit"); SwingUtilities.replaceUIActionMap(p, actionMap); SwingUtilities.replaceUIInputMap(p, JComponent.WHEN_IN_FOCUSED_WINDOW, keyMap); // ***************************************************** f.add(p); f.setVisible(true); f.pack(); } } 
+1
source

I provide this for me as a learning experience, as for anyone else. I have always had difficulty using code snippets to bind keys that I found in the past, and I hope that my explanations and code are clear. Thanks to @kleopatra for her piece of code on which I base my code below.

(I use CAPITAL LETTERS, where I DO NOT have to show more clearly what SHOULD CREATE.)

The code associates the Ctrl-Shift-U key Ctrl-Shift-U with the code in actionPerformed for MYACTION via the corresponding lines in getInputMap and getActionMap .

The three instances of MYACTION below should match, as are the four instances of MYACTIONBUTTON , as well as the two instances of the string MAKE_THESE_MATCH . Call them what you want; just make them fit.

The MYACTIONBUTTON button must have MYACTION as an argument to the JButton that defines it AND must have getInputMap and getActionMap to it.

 private static JButton MYACTIONBUTTON; private static JFrame frame; private static JPanel panel; 

...

 Action MYACTION = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { // put action code here } }; MYACTIONBUTTON = new JButton(MYACTION); MYACTIONBUTTON.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(getKeyStroke(VK_U, CTRL_DOWN_MASK | SHIFT_DOWN_MASK), "MAKE_THESE_MATCH"); MYACTIONBUTTON.getActionMap().put("MAKE_THESE_MATCH", MYACTION); panel.add(MYACTIONBUTTON); frame.add(panel); 
0
source

Source: https://habr.com/ru/post/1387352/


All Articles