public static void addKeyBinding(JComponent comp, KeyStroke key, String id, Action action) { comp.getInputMap().put(key, id); comp.getActionMap().put(id, action); }
private void addKeyActions() { KeyStroke undoKey = KeyStroke.getKeyStroke(KeyEvent.VK_Z, InputEvent.CTRL_DOWN_MASK); textComponent.getInputMap().put(undoKey, undoAction); KeyStroke redoKey = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK); textComponent.getInputMap().put(redoKey, redoAction); }
public static void installAutoTabAction(JTextComponent tComp) { tComp.getActionMap().put("GroovyFilter-autoTab", AUTO_TAB_ACTION); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); tComp.getInputMap().put(keyStroke, "GroovyFilter-autoTab"); }
protected JDialog createDialog(Component parent) { Frame frame = parent instanceof Frame ? (Frame) parent : (Frame) SwingUtilities.getAncestorOfClass(Frame.class, parent); JDialog dialog = new JDialog(frame, ("Select Font"), true); Action okAction = new DialogOKAction(dialog); Action cancelAction = new DialogCancelAction(dialog); JButton okButton = new JButton(okAction); okButton.setFont(DEFAULT_FONT); JButton cancelButton = new JButton(cancelAction); cancelButton.setFont(DEFAULT_FONT); JPanel buttonsPanel = new JPanel(); buttonsPanel.setLayout(new GridLayout(2, 1)); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); buttonsPanel.setBorder(BorderFactory.createEmptyBorder(25, 0, 10, 10)); ActionMap actionMap = buttonsPanel.getActionMap(); actionMap.put(cancelAction.getValue(Action.DEFAULT), cancelAction); actionMap.put(okAction.getValue(Action.DEFAULT), okAction); InputMap inputMap = buttonsPanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(KeyStroke.getKeyStroke("ESCAPE"), cancelAction.getValue(Action.DEFAULT)); inputMap.put(KeyStroke.getKeyStroke("ENTER"), okAction.getValue(Action.DEFAULT)); JPanel dialogEastPanel = new JPanel(); dialogEastPanel.setLayout(new BorderLayout()); dialogEastPanel.add(buttonsPanel, BorderLayout.NORTH); dialog.getContentPane().add(this, BorderLayout.CENTER); dialog.getContentPane().add(dialogEastPanel, BorderLayout.EAST); dialog.pack(); dialog.setLocationRelativeTo(frame); return dialog; }
textArea.setLineWrap( true ); textArea.setWrapStyleWord( true ); KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER"); textArea.getInputMap().put(keyStroke, "none"); JScrollPane scrollPane = new JScrollPane( textArea ); getContentPane().add( scrollPane ); buttons.add( ok ); buttons.add( cancel ); getContentPane().add(buttons, BorderLayout.SOUTH); pack(); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible(true);
public static void bindKeyToAction(int keyCode, int modifiers, Action action, JComponent component, int condition) { KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCode, modifiers); component.getInputMap(condition).put(keyStroke, action); }
private static final String TEXT_SUBMIT = "text-submit"; private static final String INSERT_BREAK = "insert-break"; ... private void initialize() { InputMap input = inputTextArea.getInputMap(); KeyStroke enter = KeyStroke.getKeyStroke("ENTER"); KeyStroke shiftEnter = KeyStroke.getKeyStroke("shift ENTER"); input.put(shiftEnter, INSERT_BREAK); // input.get(enter)) = "insert-break" input.put(enter, TEXT_SUBMIT); ActionMap actions = inputTextArea.getActionMap(); actions.put(TEXT_SUBMIT, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { submitText(); } }); } ... private void submitText() { // TODO }
private void setExitOnEscWhenEnabled(JComponent mainComponent) { Action escapeAction = new AbstractAction() { private static final long serialVersionUID = -3460391555954575248L; @Override public void actionPerformed(ActionEvent e) { if (luytenPrefs.isExitByEscEnabled()) { quit(); } } }; KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); mainComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(escapeKeyStroke, "ESCAPE"); mainComponent.getActionMap().put("ESCAPE", escapeAction); }
/** * Creates new form MessageAggregator */ public MainWindow() { keywordListModel = new DefaultListModel(); receiveMessages = new AtomicBoolean(false); initComponents(); // Bind "remove selected list items" action to DELETE key pressed in keyword list. ActionMap actionMap = keywordList.getActionMap(); InputMap inputMap = keywordList.getInputMap(JComponent.WHEN_FOCUSED); final String actionKey = "RemoveSelectedListItems"; actionMap.put(actionKey, new RemoveSelectedListItemsAction(keywordList, keywordListModel)); inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), actionKey); dataAggregators = new ArrayList<DataAggregator>(2); }
getParent().add(this, BorderLayout.CENTER); notesEditor.getDocument().addUndoableEditListener(e -> undoRedo.addEdit(e.getEdit())); notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); notesEditor.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); notesEditor.getActionMap().put("Undo", new AbstractAction("Undo") notesEditor.getActionMap().put("Redo", new AbstractAction("Redo")
/** * Add action to a single tabbedpane (ctrl-tab, ctrl-shift-tab). */ public static void addShortcut(JTabbedPane tabbedPane) { KeyStroke ctrlTab = KeyStroke.getKeyStroke("ctrl TAB"); KeyStroke ctrlShiftTab = KeyStroke.getKeyStroke("ctrl shift TAB"); // Remove ctrl-tab from normal focus traversal Set<AWTKeyStroke> forwardKeys = new HashSet<>(tabbedPane.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); forwardKeys.remove(ctrlTab); tabbedPane.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forwardKeys); // Remove ctrl-shift-tab from normal focus traversal Set<AWTKeyStroke> backwardKeys = new HashSet<>(tabbedPane.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)); backwardKeys.remove(ctrlShiftTab); tabbedPane.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backwardKeys); // Add keys to the tab's input map InputMap inputMap = tabbedPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(ctrlTab, "navigateNext"); inputMap.put(ctrlShiftTab, "navigatePrevious"); }
ActionMap actionMap = getActionMap(); int condition = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(condition); inputMap.put(direction.getKeyStroke(), direction.getText()); actionMap.put(direction.getText(), new MyArrowBinding(direction.getText())); frame.getContentPane().add(mainPanel); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); UP("Up", KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0)), DOWN("Down", KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0)), LEFT("Left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), RIGHT("Right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0));
private void setHideOnEscapeButton() { Action escapeAction = new AbstractAction() { private static final long serialVersionUID = 5572504000935312338L; @Override public void actionPerformed(ActionEvent e) { FindBox.this.setVisible(false); } }; KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE"); this.getRootPane().getActionMap().put("ESCAPE", escapeAction); }
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); updateOptions(); getContentPane().add(buttonPanel, BorderLayout.SOUTH, 1); KeyStroke k = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); Object actionKey = "cancel"; // NOI18N getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(k, actionKey); getRootPane().getActionMap().put(actionKey, cancelAction); addWindowListener( new WindowAdapter() {
/** * Removes the "Ctrl+H <=> Backspace" behavior that Java shows, for some * odd reason... */ private void fixCtrlH() { InputMap inputMap = getInputMap(); KeyStroke char010 = KeyStroke.getKeyStroke("typed \010"); InputMap parent = inputMap; while (parent != null) { parent.remove(char010); parent = parent.getParent(); } if (inputMap != null) { // Just for Sonar KeyStroke backspace = KeyStroke.getKeyStroke("BACK_SPACE"); inputMap.put(backspace, DefaultEditorKit.deletePrevCharAction); } }
this.add(b); f.getRootPane().setDefaultButton(b); this.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0), EXIT); this.getActionMap().put(EXIT, exit); this.addMouseMotionListener(new MouseAdapter() { f.setResizable(false); f.setUndecorated(true); f.add(this); f.pack(); dev.setFullScreenWindow(f);
private void setHideOnEscapeButton() { Action escapeAction = new AbstractAction() { private static final long serialVersionUID = 6846566740472934801L; @Override public void actionPerformed(ActionEvent e) { FindAllBox.this.setVisible(false); } }; KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false); this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE"); this.getRootPane().getActionMap().put("ESCAPE", escapeAction); }
public ClassListItem(Class<? extends T1> clz) { this.clz = clz; }
p.add(f); frame.add(p); frame.setVisible(true); textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released"); textField.getActionMap().put("Down released", new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) {//focuses the first label on popwindow suggestionsPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "Down released"); suggestionsPanel.getActionMap().put("Down released", new AbstractAction() { int lastFocusableIndex = 0; autoSuggestionPopUpWindow.setVisible(false); setFocusToTextField(); suggestionsPanel.add(suggestionLabel); getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), "Enter released"); getActionMap().put("Enter released", new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) {