// 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's component input map toolBarButton.getActionMap().put("myAction", action); toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction");
JToolBar bar = new JToolBar(); JButton toolBarButton = bar.add(action); toolBarButton.getActionMap().put("myAction", action); toolBarButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put( (KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "myAction");
JButton clear = new JButton("Clear"); InputMap im = clear .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap am = clear .getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "clear"); am.put("clear", new ClearFieldsAction());
public static void fixCancelButton(final javax.swing.JButton cancelButton) { cancelButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW). put(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0), "Cancel"); cancelButton.getActionMap().put("Cancel", new javax.swing.AbstractAction() { public void actionPerformed(java.awt.event.ActionEvent evt) { cancelButton.doClick(); } }); }
public static void fixOKButton(final javax.swing.JButton okButton) { okButton.getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW). put(javax.swing.KeyStroke.getKeyStroke( java.awt.event.KeyEvent.VK_ENTER, 0), "OK"); okButton.getActionMap().put("OK", new javax.swing.AbstractAction() { public void actionPerformed(java.awt.event.ActionEvent evt) { okButton.doClick(); } }); }
JButton button = new JButton(nextStepAction); button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, "enter"); button.getActionMap().put("enter", nextStepAction);
NumberAction action = new NumberAction(1); JButton btn = new JButton(action); InputMap im = btn.getInputMap(WHEN_IN_FOCUSED_WINDOW); ActionMap am = btn.getActionMap(); im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "Number1"); am.put("Number1", action);
@Override public void actionPerformed(ActionEvent e) { JButton b= button; if (b!=null && isEnabled()) { button.getActionMap().get(type).actionPerformed(e); } }
JButton btn = new JButton("Open Window"); //creates the button //registers the KeyStroke with the button btn.getInputMap().put(KeyStroke.getKeyStroke("released ENTER"), "released"); //creates an action for the component Action exit = new AbstractAction() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(btn, "You pressed the enter key"); } }; //registers the Action with the corresponding keystroke on the button component //@param exit is the action to be performed btn.getActionMap().put("released",exit);
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");
private void createButton( int x, int y, int h, int w, String actionCommand, String name, int... keys ) { nAction na = new nAction( actionCommand ); JButton btn = new JButton( na ); btn.setName( name ); InputMap im = btn.getInputMap( WHEN_IN_FOCUSED_WINDOW ); ActionMap am = btn.getActionMap(); for ( int virtualKey : keys ) { im.put( KeyStroke.getKeyStroke( virtualKey, 0, false ), "number-pressed" ); im.put( KeyStroke.getKeyStroke( virtualKey, 0, true ), "number-released" ); } am.put( "number-pressed", new NumberKeyPressedAction( btn, true ) ); am.put( "number-released", new NumberKeyPressedAction( btn, false ) ); GridBagConstraints gbc_btn = new GridBagConstraints(); // gbc_btnEquals.anchor = GridBagConstraints.WEST; gbc_btn.fill = GridBagConstraints.BOTH; gbc_btn.insets = new Insets( 0, 0, 5, 5 ); gbc_btn.gridheight = h; gbc_btn.gridwidth = w; gbc_btn.gridx = x; gbc_btn.gridy = y; frame.getContentPane().add( btn, gbc_btn ); btn.setBackground( new Color( 225, 225, 225 ) ); btn.setBorder( BorderFactory.createLineBorder( Color.BLACK ) );
/** * Creates new form SearchControlPanel. */ public SearchControlPanel() { initComponents(); nextButton.setEnabled(false); prevButton.setEnabled(false); closeButton.addActionListener(this); nextButton.addActionListener(this); prevButton.addActionListener(this); resetButton.addActionListener(this); searchField = new SearchFieldPanel(); searchField.addSearchListener(this); fieldPanel.add(searchField, BorderLayout.CENTER); //IZ 91545 findAcion = new FindAction(); nextButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0), "findNext"); //NOI18N nextButton.getActionMap().put("findNext", findAcion); //NOI18N prevButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). put(KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.SHIFT_MASK), "findPrevious"); //NOI18N prevButton.getActionMap().put("findPrevious", findAcion); }
GridLayout layout = new GridLayout(0, 4); jPanel1.setLayout(layout); Action action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.err.println("Event generated for source: "+((JButton)e.getSource()).getName()); } }; for(int i=0 ;i<4; i++) for(int j=0; j<4; j++) { String key = ""+(char)('A' + i * 4 + j); JButton button = new JButton(key); button.setName(key); button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), "doAction"); button.getActionMap().put("doAction", action); jPanel1.add(button); }
MyAction myaction = new MyAction("1"); JButton b = new JButton(myaction); b.setAction(myaction); b.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, 0), "one"); b.getActionMap().put("one", myaction);
ActionMap amap = butYes.getActionMap(); imap.put(ks, YESACTION); amap.put(YESACTION, yesAction); amap = butNo.getActionMap(); imap.put(ks, NOACTION); amap.put(NOACTION, noAction);
AbstractAction action = new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton){ JButton button = (JButton) e.getSource(); button.doClick(); } else if(e.getSource() instanceof JComponent){ JComponent component = (JComponent) e.getSource(); component.transferFocus(); } } }; JTextField textField1 = new JTextField(); textField1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); textField1.getActionMap().put("TransferFocus", action); JTextField textField2 = new JTextField(); textField2.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "TransferFocus"); textField2.getActionMap().put("TransferFocus", action); JButton button1= new JButton(); button1.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter"); button1.getActionMap().put("Enter", action);
jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY); jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() { private static final long serialVersionUID = 1L;
jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY); jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() { private static final long serialVersionUID = 1L;
jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), CANCEL_KEY); jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() { private static final long serialVersionUID = 1L;
public static void bindKeyStroke(final JButton btn, String ks) { final ActionListener[] alist = btn.getActionListeners(); if (alist.length != 0) { AbstractAction action = new AbstractAction(btn.getText(), btn.getIcon()) { @Override public void actionPerformed(ActionEvent e) { for (ActionListener al : alist) { ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), Action.ACCELERATOR_KEY); al.actionPerformed(ae); } } }; KeyStroke keyStroke = KeyStroke.getKeyStroke(ks); btn.setAction(action); btn.getActionMap().put(Action.ACCELERATOR_KEY, action); btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(keyStroke, Action.ACCELERATOR_KEY); } }