this.setPreferredSize(new Dimension(640, 480)); this.addMouseListener(mouseHandler); this.addMouseMotionListener(mouseHandler); 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() { JFrame f = new JFrame("LinePanel"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.add(new ControlPanel(), BorderLayout.SOUTH); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true);
@Override public Object invoke(Object o, Method method, Object[] args) throws Throwable { if (method.getName().equals("windowEnteredFullScreen")) { putValue(NAME, LocalisationHelper.getString("main_frame_menuitem_leave_fullscreen")); putValue(SHORT_DESCRIPTION, LocalisationHelper.getString("main_frame_menuitem_hint_leave_fullscreen")); gcViewer.getDesktopPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "leaveFullScreen"); gcViewer.getDesktopPane().getActionMap().put("leaveFullScreen", OSXFullScreen.this); } if (method.getName().equals("windowExitedFullScreen")) { putValue(NAME, LocalisationHelper.getString("main_frame_menuitem_enter_fullscreen")); putValue(SHORT_DESCRIPTION, LocalisationHelper.getString("main_frame_menuitem_hint_enter_fullscreen")); gcViewer.getDesktopPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).remove(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0)); gcViewer.getDesktopPane().getActionMap().remove("leaveFullScreen"); } return null; } });
Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add( nullTraversalKeys() ); contentPane.add( writeYourOwnAction() ); contentPane.add( useKeyListener() ); contentPane.add( addTraversalKeys() ); textArea.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null); textArea.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null); 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)); frame.setDefaultCloseOperation( EXIT_ON_CLOSE ); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true);
Action action = null; do { action = action == null ? aMap.get(DefaultEditorKit.deletePrevCharAction) : null; aMap.remove(DefaultEditorKit.deletePrevCharAction); aMap = aMap.getParent(); } while (aMap != null); 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"); keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_MASK, false); iMap.put(keyStroke, FIND); iMap.put(keyStroke, FIND_NEXT); iMap.put(keyStroke, FIND_PREVIOUS); iMap.put(keyStroke, "TextEditor-tabAction"); iMap.put(keyStroke, "TextEditor-shiftTabAction"); iMap.remove(keyStroke); iMap = iMap.getParent(); } while (iMap != null); getInputMap().put(keyStroke, REPLACE);
JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); p.add(f); frame.add(p); frame.pack(); frame.setVisible(true); autoSuggestionPopUpWindow.setOpacity(opacity); suggestionsPanel.setLayout(new GridLayout(0, 1)); suggestionsPanel.setBackground(popUpBackground); 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; 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 = 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())); ArrowTest mainPanel = new ArrowTest(); JFrame frame = new JFrame("ArrowTest"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 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));
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; }
editorComponent.setBackground(Color.white); editorComponent.setBorderPainted(false); editorComponent.setContentAreaFilled( false ); popup.setLocation(p.x, p.y + editorComponent.getSize().height); popup.show(); fireEditingStopped(); textArea.setLineWrap( true ); textArea.setWrapStyleWord( true ); KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER"); textArea.getInputMap().put(keyStroke, "none"); JScrollPane scrollPane = new JScrollPane( textArea ); getContentPane().add( scrollPane ); ok.setPreferredSize( cancel.getPreferredSize() ); ok.addActionListener( this ); buttons.add( ok ); buttons.add( cancel ); getContentPane().add(buttons, BorderLayout.SOUTH); pack(); table.getColumnModel().getColumn(1).setCellEditor( popupEditor );
private JFrame f = new JFrame("FullScreenTest"); private Action exit = new AbstractAction(EXIT) { 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() { FullScreenTest.this.setToolTipText( "("+ e.getX() + "," + e.getY() + ")"); GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice dev = env.getDefaultScreenDevice(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setBackground(Color.darkGray); f.setResizable(false); f.setUndecorated(true); f.add(this); f.pack(); dev.setFullScreenWindow(f);
public void run() { JFrame frame = new JFrame(pTitle); frame.getRootPane().getActionMap().put("window-close", new AbstractAction() { public void actionPerformed(ActionEvent e) { Window window = SwingUtilities.getWindowAncestor((Component) e.getSource()); window.setVisible(false); window.dispose(); } }); frame.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "window-close"); frame.addWindowListener(new ExitIfNoWindowPresentHandler()); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationByPlatform(true); JPanel pane = new JPanel(new BorderLayout()); JScrollPane scroll = new JScrollPane(pImage != null ? new ImageLabel(pImage) : new JLabel("(no image data)", JLabel.CENTER)); scroll.setBorder(null); pane.add(scroll); frame.setContentPane(pane); frame.pack(); frame.setVisible(true); } });
public static void addKeyBinding(JComponent comp, KeyStroke key, String id, Action action) { comp.getInputMap().put(key, id); comp.getActionMap().put(id, action); }
/** {@inheritDoc} */ public void showMainWindow() { frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setMinimumSize(new Dimension(800, 600)); frame.setPreferredSize(new Dimension(1000, 800)); layout = new DesktopAppLayout(); frame.getContentPane().setLayout(layout); frame.setVisible(true); JRootPane rootPane = frame.getRootPane(); rootPane.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "pressed"); }
final JFrame frame = new JFrame("Keys"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_H, InputEvent.ALT_MASK); frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "TestAction"); frame.getRootPane().getActionMap().put("TestAction", testAction); label.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); frame.add(label); frame.setLocationByPlatform(true); frame.pack(); frame.setVisible(true);
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"); }
import java.awt.event.*; import javax.swing.*; public class TestShiftCtrlSpace { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable(){ public void run() { JFrame frame = new JFrame(); JPanel panel = (JPanel)frame.getContentPane(); InputMap im = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); im.put(KeyStroke.getKeyStroke("shift ctrl pressed SPACE"), "scs"); panel.getActionMap().put("scs", new AbstractAction(){ public void actionPerformed(ActionEvent e) { System.out.println("Shift + Ctrl + Space"); } }); frame.setSize(300, 300); frame.setLocationByPlatform(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }); } }
JFrame frame = new JFrame(); JPanel comp = new JPanel(); KeyStroke keyStroke = KeyStroke.getKeyStroke(keyCombination); textField.getInputMap().put(keyStroke, keyCombination); textField.getActionMap().put(keyCombination, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { }}); comp.add(textField); frame.add(comp); frame.setSize(new Dimension(500,500)); frame.setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); setLayout(new BorderLayout()); setVisible(true); panel = new JPanel(); panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0), "forward"); panel.getActionMap().put("forward", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { add(panel, BorderLayout.CENTER);
@PostConstruct protected void init() { final KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_L, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); final String actionName = "org.wiztools.restclient:ADDRESS_FOCUS"; jcb_url.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(ks, actionName); jcb_url.getActionMap().put(actionName, new AbstractAction() { @Override public void actionPerformed(ActionEvent ae) { jl_url.setLabelFor(jcb_url); jl_url.setDisplayedMnemonic('u'); rest_ui.getFrame().getRootPane().setDefaultButton(jb_request); rest_ui.getFrame().getRootPane().setDefaultButton(jb_request); jb_request.addActionListener(new ActionListener() { @Override
/** * 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); } }
Listener() { addWindowFocusListener(this); list.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount()==2) { insertSelectedItem(); } } }); list.getInputMap().put( KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "onEnter"); list.getActionMap().put("onEnter", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { insertSelectedItem(); } }); // If anything happens to the "parent" window, hide this popup Window parent = (Window)getParent(); parent.addWindowFocusListener(this); parent.addWindowListener(this); parent.addComponentListener(this); }