List of usage examples for javax.swing InputMap put
public void put(KeyStroke keyStroke, Object actionMapKey)
From source file:Main.java
public static void main(String args[]) { String ACTION_KEY = "The Action"; JFrame frame = new JFrame("KeyStroke Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton buttonA = new JButton("FOCUSED (control alt 7)"); JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)"); JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)"); JButton buttonD = new JButton("WINDOW (' ')"); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }/*from w w w .j a v a 2 s .co m*/ }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); ActionMap newMap = new ActionMap(); newMap.setParent(actionMap); System.out.println(newMap.getParent()); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); frame.setLayout(new GridLayout(2, 2)); frame.add(buttonA); frame.add(buttonB); frame.add(buttonC); frame.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }
From source file:FrameKey.java
public static void main(String args[]) { final JFrame frame = new JFrame("Frame Key"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JDialog dialog = new EscapeDialog(frame, "Hey"); JButton button = new JButton("Okay"); ActionListener innerActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Dialog Button Selected"); }//from w ww.j a v a 2s .c om }; button.addActionListener(innerActionListener); dialog.getContentPane().add(button, BorderLayout.SOUTH); dialog.setSize(200, 200); dialog.show(); } }; JPanel content = (JPanel) frame.getContentPane(); KeyStroke stroke = KeyStroke.getKeyStroke("M"); InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(stroke, "OPEN"); content.getActionMap().put("OPEN", actionListener); frame.setSize(300, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("Frame"); frame.add(Box.createRigidArea(new Dimension(400, 300))); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack();//from ww w . j a v a 2 s .co m frame.setVisible(true); JDialog dialog = new JDialog(frame, "Dialog", true); int condition = JPanel.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = ((JPanel) dialog.getContentPane()).getInputMap(condition); ActionMap actionMap = ((JPanel) dialog.getContentPane()).getActionMap(); String enter = "enter"; inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), enter); actionMap.put(enter, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { dialog.dispose(); } }); dialog.add(Box.createRigidArea(new Dimension(200, 200))); dialog.pack(); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); }
From source file:MoveViewSample.java
public static void main(String args[]) { JFrame frame = new JFrame("JViewport Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon icon = new ImageIcon("yourFile.gif"); JLabel dogLabel = new JLabel(icon); JViewport viewport = new JViewport(); viewport.setView(dogLabel);//ww w .j a v a2s .co m InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = viewport.getActionMap(); Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT); KeyStroke upKey = KeyStroke.getKeyStroke("UP"); inputMap.put(upKey, "up"); actionMap.put("up", upKeyAction); Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT); KeyStroke downKey = KeyStroke.getKeyStroke("DOWN"); inputMap.put(downKey, "down"); actionMap.put("down", downKeyAction); Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT); KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT"); inputMap.put(leftKey, "left"); actionMap.put("left", leftKeyAction); Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT); KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT"); inputMap.put(rightKey, "right"); actionMap.put("right", rightKeyAction); Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK); KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP"); inputMap.put(pgUpKey, "pgUp"); actionMap.put("pgUp", pgUpKeyAction); Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK); KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN"); inputMap.put(pgDnKey, "pgDn"); actionMap.put("pgDn", pgDnKeyAction); Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK); KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP"); inputMap.put(shiftPgUpKey, "shiftPgUp"); actionMap.put("shiftPgUp", shiftPgUpKeyAction); Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK); KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN"); inputMap.put(shiftPgDnKey, "shiftPgDn"); actionMap.put("shiftPgDn", shiftPgDnKeyAction); frame.add(viewport, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:MoveViewSample.java
public static void main(String args[]) { JFrame frame = new JFrame("JViewport Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Icon icon = new ImageIcon("dog.jpg"); JLabel dogLabel = new JLabel(icon); JViewport viewport = new JViewport(); viewport.setView(dogLabel);//from w ww . ja v a2 s . c om InputMap inputMap = viewport.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = viewport.getActionMap(); // Up key moves view up unit Action upKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, UNIT); KeyStroke upKey = KeyStroke.getKeyStroke("UP"); inputMap.put(upKey, "up"); actionMap.put("up", upKeyAction); // Down key moves view down unit Action downKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, UNIT); KeyStroke downKey = KeyStroke.getKeyStroke("DOWN"); inputMap.put(downKey, "down"); actionMap.put("down", downKeyAction); // Left key moves view left unit Action leftKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, UNIT); KeyStroke leftKey = KeyStroke.getKeyStroke("LEFT"); inputMap.put(leftKey, "left"); actionMap.put("left", leftKeyAction); // Right key moves view right unit Action rightKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, UNIT); KeyStroke rightKey = KeyStroke.getKeyStroke("RIGHT"); inputMap.put(rightKey, "right"); actionMap.put("right", rightKeyAction); // PgUp key moves view up block Action pgUpKeyAction = new MoveAction(viewport, DECREASE, Y_AXIS, BLOCK); KeyStroke pgUpKey = KeyStroke.getKeyStroke("PAGE_UP"); inputMap.put(pgUpKey, "pgUp"); actionMap.put("pgUp", pgUpKeyAction); // PgDn key moves view down block Action pgDnKeyAction = new MoveAction(viewport, INCREASE, Y_AXIS, BLOCK); KeyStroke pgDnKey = KeyStroke.getKeyStroke("PAGE_DOWN"); inputMap.put(pgDnKey, "pgDn"); actionMap.put("pgDn", pgDnKeyAction); // Shift-PgUp key moves view left block Action shiftPgUpKeyAction = new MoveAction(viewport, DECREASE, X_AXIS, BLOCK); KeyStroke shiftPgUpKey = KeyStroke.getKeyStroke("shift PAGE_UP"); inputMap.put(shiftPgUpKey, "shiftPgUp"); actionMap.put("shiftPgUp", shiftPgUpKeyAction); // Shift-PgDn key moves view right block Action shiftPgDnKeyAction = new MoveAction(viewport, INCREASE, X_AXIS, BLOCK); KeyStroke shiftPgDnKey = KeyStroke.getKeyStroke("shift PAGE_DOWN"); inputMap.put(shiftPgDnKey, "shiftPgDn"); actionMap.put("shiftPgDn", shiftPgDnKeyAction); Container contentPane = frame.getContentPane(); contentPane.add(viewport, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:com.willwinder.universalgcodesender.ExperimentalWindow.java
/** * @param args the command line arguments *//*from w ww .j ava 2s . c o m*/ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(ExperimentalWindow.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> // Fix look and feel to use CMD+C/X/V/A instead of CTRL if (SystemUtils.IS_OS_MAC) { Collection<InputMap> ims = new ArrayList<>(); ims.add((InputMap) UIManager.get("TextField.focusInputMap")); ims.add((InputMap) UIManager.get("TextArea.focusInputMap")); ims.add((InputMap) UIManager.get("EditorPane.focusInputMap")); ims.add((InputMap) UIManager.get("FormattedTextField.focusInputMap")); ims.add((InputMap) UIManager.get("PasswordField.focusInputMap")); ims.add((InputMap) UIManager.get("TextPane.focusInputMap")); int c = KeyEvent.VK_C; int v = KeyEvent.VK_V; int x = KeyEvent.VK_X; int a = KeyEvent.VK_A; int meta = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); for (InputMap im : ims) { im.put(KeyStroke.getKeyStroke(c, meta), DefaultEditorKit.copyAction); im.put(KeyStroke.getKeyStroke(v, meta), DefaultEditorKit.pasteAction); im.put(KeyStroke.getKeyStroke(x, meta), DefaultEditorKit.cutAction); im.put(KeyStroke.getKeyStroke(a, meta), DefaultEditorKit.selectAllAction); } } /* Create the form */ // GUIBackend backend = new GUIBackend(); final ExperimentalWindow mw = new ExperimentalWindow(); /* Apply the settings to the ExperimentalWindow bofore showing it */ mw.setSize(mw.backend.getSettings().getMainWindowSettings().width, mw.backend.getSettings().getMainWindowSettings().height); mw.setLocation(mw.backend.getSettings().getMainWindowSettings().xLocation, mw.backend.getSettings().getMainWindowSettings().yLocation); mw.addComponentListener(new ComponentListener() { @Override public void componentResized(ComponentEvent ce) { mw.backend.getSettings().getMainWindowSettings().height = ce.getComponent().getSize().height; mw.backend.getSettings().getMainWindowSettings().width = ce.getComponent().getSize().width; } @Override public void componentMoved(ComponentEvent ce) { mw.backend.getSettings().getMainWindowSettings().xLocation = ce.getComponent().getLocation().x; mw.backend.getSettings().getMainWindowSettings().yLocation = ce.getComponent().getLocation().y; } @Override public void componentShown(ComponentEvent ce) { } @Override public void componentHidden(ComponentEvent ce) { } }); /* Display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { mw.setVisible(true); } }); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { mw.connectionPanel.saveSettings(); mw.commandPanel.saveSettings(); if (mw.pendantUI != null) { mw.pendantUI.stop(); } } }); }
From source file:com.willwinder.universalgcodesender.MainWindow.java
/** * @param args the command line arguments *//*from ww w . ja v a 2 s .co m*/ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> // Fix look and feel to use CMD+C/X/V/A instead of CTRL if (SystemUtils.IS_OS_MAC) { Collection<InputMap> ims = new ArrayList<>(); ims.add((InputMap) UIManager.get("TextField.focusInputMap")); ims.add((InputMap) UIManager.get("TextArea.focusInputMap")); ims.add((InputMap) UIManager.get("EditorPane.focusInputMap")); ims.add((InputMap) UIManager.get("FormattedTextField.focusInputMap")); ims.add((InputMap) UIManager.get("PasswordField.focusInputMap")); ims.add((InputMap) UIManager.get("TextPane.focusInputMap")); int c = KeyEvent.VK_C; int v = KeyEvent.VK_V; int x = KeyEvent.VK_X; int a = KeyEvent.VK_A; int meta = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); for (InputMap im : ims) { im.put(KeyStroke.getKeyStroke(c, meta), DefaultEditorKit.copyAction); im.put(KeyStroke.getKeyStroke(v, meta), DefaultEditorKit.pasteAction); im.put(KeyStroke.getKeyStroke(x, meta), DefaultEditorKit.cutAction); im.put(KeyStroke.getKeyStroke(a, meta), DefaultEditorKit.selectAllAction); } } /* Create the form */ GUIBackend backend = new GUIBackend(); final MainWindow mw = new MainWindow(backend); /* Apply the settings to the MainWindow bofore showing it */ mw.arrowMovementEnabled.setSelected(mw.settings.isManualModeEnabled()); mw.stepSizeSpinner.setValue(mw.settings.getManualModeStepSize()); boolean unitsAreMM = mw.settings.getDefaultUnits().equals("mm"); mw.mmRadioButton.setSelected(unitsAreMM); mw.inchRadioButton.setSelected(!unitsAreMM); mw.fileChooser = new JFileChooser(mw.settings.getLastOpenedFilename()); mw.commPortComboBox.setSelectedItem(mw.settings.getPort()); mw.baudrateSelectionComboBox.setSelectedItem(mw.settings.getPortRate()); mw.scrollWindowCheckBox.setSelected(mw.settings.isScrollWindowEnabled()); mw.showVerboseOutputCheckBox.setSelected(mw.settings.isVerboseOutputEnabled()); mw.showCommandTableCheckBox.setSelected(mw.settings.isCommandTableEnabled()); mw.showCommandTableCheckBoxActionPerformed(null); mw.firmwareComboBox.setSelectedItem(mw.settings.getFirmwareVersion()); mw.setSize(mw.settings.getMainWindowSettings().width, mw.settings.getMainWindowSettings().height); mw.setLocation(mw.settings.getMainWindowSettings().xLocation, mw.settings.getMainWindowSettings().yLocation); mw.addComponentListener(new ComponentListener() { @Override public void componentResized(ComponentEvent ce) { mw.settings.getMainWindowSettings().height = ce.getComponent().getSize().height; mw.settings.getMainWindowSettings().width = ce.getComponent().getSize().width; } @Override public void componentMoved(ComponentEvent ce) { mw.settings.getMainWindowSettings().xLocation = ce.getComponent().getLocation().x; mw.settings.getMainWindowSettings().yLocation = ce.getComponent().getLocation().y; } @Override public void componentShown(ComponentEvent ce) { } @Override public void componentHidden(ComponentEvent ce) { } }); /* Display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { mw.setVisible(true); } }); mw.initFileChooser(); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (mw.fileChooser.getSelectedFile() != null) { mw.settings.setLastOpenedFilename(mw.fileChooser.getSelectedFile().getAbsolutePath()); } mw.settings.setDefaultUnits(mw.inchRadioButton.isSelected() ? "inch" : "mm"); mw.settings.setManualModeStepSize(mw.getStepSize()); mw.settings.setManualModeEnabled(mw.arrowMovementEnabled.isSelected()); mw.settings.setPort(mw.commPortComboBox.getSelectedItem().toString()); mw.settings.setPortRate(mw.baudrateSelectionComboBox.getSelectedItem().toString()); mw.settings.setScrollWindowEnabled(mw.scrollWindowCheckBox.isSelected()); mw.settings.setVerboseOutputEnabled(mw.showVerboseOutputCheckBox.isSelected()); mw.settings.setCommandTableEnabled(mw.showCommandTableCheckBox.isSelected()); mw.settings.setFirmwareVersion(mw.firmwareComboBox.getSelectedItem().toString()); SettingsFactory.saveSettings(mw.settings); if (mw.pendantUI != null) { mw.pendantUI.stop(); } } }); // Check command line for a file to open. boolean open = false; for (String arg : args) { if (open) { try { backend.setGcodeFile(new File(arg)); } catch (Exception ex) { Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex); System.exit(1); } } if (arg.equals("--open") || arg.equals("-o")) { open = true; } } }
From source file:Main.java
public static void closeOnEscape(final JFrame frame) { KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE"); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { frame.setVisible(false);/* w w w . ja v a 2s . co m*/ frame.dispose(); } }; JRootPane rootPane = frame.getRootPane(); InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(stroke, "ESCAPE"); rootPane.getActionMap().put("ESCAPE", actionListener); }
From source file:Main.java
/** * Binds an action to the dialog so when the user presses the ESCAPE key, the dialog is hidden. * /*ww w. j a va 2s. c o m*/ * @param dialog * the dialog to bind the action to. */ public static void bindEscapeAction(final JDialog dialog) { InputMap iMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape"); ActionMap aMap = dialog.getRootPane().getActionMap(); aMap.put("escape", new AbstractAction() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); }
From source file:Main.java
/** * install focus forward and backward/*from w w w .j a va 2 s .c o m*/ */ public static void installDefaultFocusHandling(Container c) { // focus TAB HashSet<KeyStroke> set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set); // focus shift-TAB set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set); // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work // correctly if (c instanceof JComponent) { JComponent jc = (JComponent) c; InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED); if (inputMapWhenFocused.size() == 0) { inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED), "swingDummyFocusKey"); } } }