List of usage examples for javax.swing KeyStroke getKeyStroke
public static KeyStroke getKeyStroke(String s)
KeyStroke
. From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent textcomp = new JTextArea(); final UndoManager undo = new UndoManager(); Document doc = textcomp.getDocument(); JFrame f = new JFrame(); f.add(new JScrollPane(textcomp)); f.setSize(330, 300);/*from w w w. j av a 2 s . com*/ f.setVisible(true); doc.addUndoableEditListener(new UndoableEditListener() { public void undoableEditHappened(UndoableEditEvent evt) { undo.addEdit(evt.getEdit()); } }); textcomp.getActionMap().put("Undo", new AbstractAction("Undo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canUndo()) { undo.undo(); } } catch (CannotUndoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo"); textcomp.getActionMap().put("Redo", new AbstractAction("Redo") { public void actionPerformed(ActionEvent evt) { try { if (undo.canRedo()) { undo.redo(); } } catch (CannotRedoException e) { } } }); textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo"); }
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 w w .j a va 2 s . c o m }; 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:MediumPopupSample.java
public static void main(String args[]) { ActionListener menuListener = new MenuActionListener(); JFrame frame = new JFrame("MenuSample Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar = new JMenuBar(); // File Menu, F - Mnemonic JMenu fileMenu = new JMenu("File"); fileMenu.setMnemonic(KeyEvent.VK_F); menuBar.add(fileMenu);/*from w ww . j a v a2 s . c o m*/ // File->New, N - Mnemonic JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N); newMenuItem.addActionListener(menuListener); fileMenu.add(newMenuItem); // File->Open, O - Mnemonic JMenuItem openMenuItem = new JMenuItem("Open", KeyEvent.VK_O); openMenuItem.addActionListener(menuListener); fileMenu.add(openMenuItem); // File->Close, C - Mnemonic JMenuItem closeMenuItem = new JMenuItem("Close", KeyEvent.VK_C); closeMenuItem.addActionListener(menuListener); fileMenu.add(closeMenuItem); // Separator fileMenu.addSeparator(); // File->Save, S - Mnemonic JMenuItem saveMenuItem = new JMenuItem("Save", KeyEvent.VK_S); saveMenuItem.addActionListener(menuListener); fileMenu.add(saveMenuItem); // Separator fileMenu.addSeparator(); // File->Exit, X - Mnemonic JMenuItem exitMenuItem = new JMenuItem("Exit", KeyEvent.VK_X); exitMenuItem.addActionListener(menuListener); fileMenu.add(exitMenuItem); // Edit Menu, E - Mnemonic JMenu editMenu = new JMenu("Edit"); editMenu.setMnemonic(KeyEvent.VK_E); menuBar.add(editMenu); // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator JMenuItem cutMenuItem = new JMenuItem("Cut", KeyEvent.VK_T); cutMenuItem.addActionListener(menuListener); KeyStroke ctrlXKeyStroke = KeyStroke.getKeyStroke("control X"); cutMenuItem.setAccelerator(ctrlXKeyStroke); editMenu.add(cutMenuItem); // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator JMenuItem copyMenuItem = new JMenuItem("Copy", KeyEvent.VK_C); copyMenuItem.addActionListener(menuListener); KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke("control C"); copyMenuItem.setAccelerator(ctrlCKeyStroke); editMenu.add(copyMenuItem); // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled JMenuItem pasteMenuItem = new JMenuItem("Paste", KeyEvent.VK_P); pasteMenuItem.addActionListener(menuListener); KeyStroke ctrlVKeyStroke = KeyStroke.getKeyStroke("control V"); pasteMenuItem.setAccelerator(ctrlVKeyStroke); pasteMenuItem.setEnabled(false); editMenu.add(pasteMenuItem); // Separator editMenu.addSeparator(); // Edit->Find, F - Mnemonic, F3 - Accelerator JMenuItem findMenuItem = new JMenuItem("Find", KeyEvent.VK_F); findMenuItem.addActionListener(menuListener); KeyStroke f3KeyStroke = KeyStroke.getKeyStroke("F3"); findMenuItem.setAccelerator(f3KeyStroke); editMenu.add(findMenuItem); // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File JMenu findOptionsMenu = new JMenu("Options"); Icon atIcon = new ImageIcon("at.gif"); findOptionsMenu.setIcon(atIcon); findOptionsMenu.setMnemonic(KeyEvent.VK_O); // ButtonGroup for radio buttons ButtonGroup directionGroup = new ButtonGroup(); // Edit->Options->Forward, F - Mnemonic, in group JRadioButtonMenuItem forwardMenuItem = new JRadioButtonMenuItem("Forward", true); forwardMenuItem.addActionListener(menuListener); forwardMenuItem.setMnemonic(KeyEvent.VK_F); findOptionsMenu.add(forwardMenuItem); directionGroup.add(forwardMenuItem); // Edit->Options->Backward, B - Mnemonic, in group JRadioButtonMenuItem backwardMenuItem = new JRadioButtonMenuItem("Backward"); backwardMenuItem.addActionListener(menuListener); backwardMenuItem.setMnemonic(KeyEvent.VK_B); findOptionsMenu.add(backwardMenuItem); directionGroup.add(backwardMenuItem); // Separator findOptionsMenu.addSeparator(); // Edit->Options->Case Sensitive, C - Mnemonic JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive"); caseMenuItem.addActionListener(menuListener); caseMenuItem.setMnemonic(KeyEvent.VK_C); findOptionsMenu.add(caseMenuItem); editMenu.add(findOptionsMenu); frame.setJMenuBar(menuBar); frame.setSize(350, 250); 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("yourFile.gif"); JLabel dogLabel = new JLabel(icon); JViewport viewport = new JViewport(); viewport.setView(dogLabel);// w ww .j av a 2 s . 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);//w ww.ja v a 2 s . c o m 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: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);//from w w w .j a va 2 s. c om 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
public static void addAction(JComponent component, String keyStroke, AbstractAction action) { KeyStroke ks = KeyStroke.getKeyStroke(keyStroke); if (ks == null) throw new IllegalArgumentException("invalid key stroke: " + keyStroke); Object key = ks + "-" + System.currentTimeMillis(); component.getActionMap().put(key, action); component.getInputMap().put(ks, key); }
From source file:Main.java
public static void addAction(JComponent component, String kstr, AbstractAction action) { KeyStroke ks = KeyStroke.getKeyStroke(kstr); component.getInputMap().put(ks, ks); component.getActionMap().put(ks, action); }
From source file:Main.java
public static void associaTeclaAtalho(JComponent component, Action action, String nomeAcao, String... atalhos) { final InputMap inputMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); final ActionMap actionMap = component.getActionMap(); for (String atalho : atalhos) { KeyStroke keyStroke = KeyStroke.getKeyStroke(atalho); inputMap.put(keyStroke, nomeAcao); actionMap.put(nomeAcao, action); }//from www . ja v a2s . co m }
From source file:Main.java
public static void installAlternateCopyPaste(JComponent comp) { comp.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift INSERT"), "paste-from-clipboard"); comp.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("shift DELETE"), "cut-to-clipboard"); comp.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("ctrl INSERT"), "copy-to-clipboard"); }