public static void addKeyBinding(JComponent comp, KeyStroke key, String id, Action action) { comp.getInputMap().put(key, id); comp.getActionMap().put(id, action); }
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; }
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")
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() {
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) {
ActionMap actionMap = textConponent.getActionMap(); InputMap inputMap = textConponent.getInputMap(); actionMap.put("Undo", new AbstractAction("Undo") { @Override public void actionPerformed(ActionEvent evt) { inputMap.put(KeyStroke.getKeyStroke("control Z"), "Undo"); actionMap.put("Redo", new AbstractAction("Redo") { @Override public void actionPerformed(ActionEvent evt) { inputMap.put(KeyStroke.getKeyStroke("control Y"), "Redo");
@Override protected JDialog createDialog(Component parent) throws HeadlessException { final JDialog dialog = super.createDialog(parent); final JComponent selector = createDirectorySelector(this); //Close dialog when escape is pressed InputMap in = dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); in.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0), "escape"); ActionMap aMap = dialog.getRootPane().getActionMap(); aMap.put("escape", new AbstractAction() { @Override public void actionPerformed (ActionEvent e) { dialog.dispose(); } }); if(selector != null){ dialog.getContentPane().add(selector, BorderLayout.NORTH); dialog.pack(); } return dialog; }
this.add(new MoveButton("\u2190", KeyEvent.VK_LEFT, -DELTA, 0)); this.add(new MoveButton("\u2191", KeyEvent.VK_UP, 0, -DELTA)); this.add(new MoveButton("\u2192", KeyEvent.VK_RIGHT, DELTA, 0)); this.add(new MoveButton("\u2193", KeyEvent.VK_DOWN, 0, DELTA)); final int dx, final int dy) { super(name); this.k = KeyStroke.getKeyStroke(code, 0); this.dx = dx; this.dy = dy; ControlPanel.this.getInputMap(WHEN_IN_FOCUSED_WINDOW) .put(k, k.toString()); ControlPanel.this.getActionMap() .put(k.toString(), new AbstractAction() { f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
aMap = getActionMap(); InputMap iMap = getInputMap(); KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0, false); iMap.put(keyStroke, "delete"); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, KeyEvent.SHIFT_MASK, false); iMap.put(keyStroke, "delete"); aMap.put("delete", action); aMap.put(FIND, action); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK, false); iMap.put(keyStroke, FIND); aMap.put(FIND_NEXT, FindReplaceUtility.FIND_ACTION); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0, false); iMap.put(keyStroke, FIND_NEXT); aMap.put(FIND_PREVIOUS, FindReplaceUtility.FIND_ACTION); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F3, KeyEvent.SHIFT_MASK, false); iMap.put(keyStroke, FIND_PREVIOUS); aMap.put("TextEditor-tabAction", action); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0, false); iMap.put(keyStroke, "TextEditor-tabAction"); aMap.put("TextEditor-shiftTabAction", action); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_TAB, KeyEvent.SHIFT_MASK, false); iMap.put(keyStroke, "TextEditor-shiftTabAction"); getActionMap().put(REPLACE, action);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add( nullTraversalKeys() ); contentPane.add( writeYourOwnAction() ); contentPane.add( useKeyListener() ); contentPane.add( addTraversalKeys() ); JScrollPane scrollPane = new JScrollPane( textArea ); InputMap im = textArea.getInputMap(); KeyStroke tab = KeyStroke.getKeyStroke("TAB"); textArea.getActionMap().put(im.get(tab), new TabAction(true)); KeyStroke shiftTab = KeyStroke.getKeyStroke("shift TAB"); im.put(shiftTab, shiftTab); textArea.getActionMap().put(im.get(shiftTab), new TabAction(false)); set.add( KeyStroke.getKeyStroke( "TAB" ) ); textArea.setFocusTraversalKeys( KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set ); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);