List of usage examples for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW
int WHEN_IN_FOCUSED_WINDOW
To view the source code for javax.swing JComponent WHEN_IN_FOCUSED_WINDOW.
Click Source Link
registerKeyboardAction
that means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component. From source file:Main.java
public static void main(String[] argv) throws Exception { JButton component = new JButton(); MyAction action = new MyAction(); component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("F2"), action.getValue(Action.NAME)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JButton component = new JButton("button"); InputMap map = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); list(map, map.keys());/*from w w w .ja v a2s . c om*/ list(map, map.allKeys()); }
From source file:MainClass.java
public static void main(final String args[]) { final JFrame frame = new JFrame("Frame Key"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Got an M"); }/*from ww w . j a v a 2s . c o m*/ }; 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[]) { JPanel JMainPanel = new JPanel(new BorderLayout()); JPanel jp = new JPanel(); JComboBox combo = new JComboBox(new String[] { "Item1", "Item2", "Item3" }); JPanel jImage = new JPanel(); JFrame jf = new JFrame(); jp.add(combo);//from w w w . j ava 2 s. c o m JMainPanel.add(jp, BorderLayout.WEST); JMainPanel.add(jImage, BorderLayout.CENTER); jp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.ALT_DOWN_MASK), "screenshot"); jp.getActionMap().put("screenshot", new AbstractAction() { @Override public void actionPerformed(ActionEvent arg0) { final BufferedImage bf = new BufferedImage(400, 400, BufferedImage.TYPE_INT_RGB); javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { jf.getRootPane().paint(bf.getGraphics()); jImage.getGraphics().drawImage(bf, 0, 0, jImage); } }); } }); jf.getContentPane().add(JMainPanel); jf.setSize(500, 500); jf.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JLabel("Hit Escape to exit full screen", JLabel.CENTER), BorderLayout.CENTER); frame.setSize(300, 300);//from w ww . j av a 2 s . com KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke("ESCAPE"); Action escapeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice() .setFullScreenWindow(null); } }; frame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE"); frame.getRootPane().getActionMap().put("ESCAPE", escapeAction); GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(frame); }
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 va2s .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:KeyTester.java
public static void main(String args[]) { String actionKey = "theAction"; JFrame f = new JFrame("Key Tester"); JButton jb1 = new JButton("<html><center>B<br>Focused/Typed"); JButton jb2 = new JButton("<html><center>Ctrl-C<br>Window/Pressed"); JButton jb3 = new JButton("<html><center>Shift-D<br>Ancestor/Released"); Container pane = f.getContentPane(); pane.add(jb1, BorderLayout.NORTH); pane.add(jb2, BorderLayout.CENTER); pane.add(jb3, BorderLayout.SOUTH); KeyStroke stroke = KeyStroke.getKeyStroke("typed B"); Action action = new MyActionListener("Action Happened"); // Defaults to JComponent.WHEN_FOCUSED map InputMap inputMap = jb1.getInputMap(); inputMap.put(stroke, actionKey);/*from www . j a v a 2 s . com*/ ActionMap actionMap = jb1.getActionMap(); actionMap.put(actionKey, action); stroke = KeyStroke.getKeyStroke("ctrl C"); action = new MyActionListener("Action Didn't Happen"); inputMap = jb2.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(stroke, actionKey); actionMap = jb2.getActionMap(); actionMap.put(actionKey, action); stroke = KeyStroke.getKeyStroke("shift released D"); action = new MyActionListener("What Happened?"); inputMap = jb3.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(stroke, actionKey); actionMap = jb3.getActionMap(); actionMap.put(actionKey, action); f.setSize(200, 200); f.show(); }
From source file:ColorAction.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setTitle("SeparateGUITest"); frame.setSize(300, 200);/*from w w w . j a va2 s . c o m*/ frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JPanel panel = new JPanel(); Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.blue, panel); Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.yellow, panel); Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.red, panel); panel.add(new JButton(yellowAction)); panel.add(new JButton(blueAction)); panel.add(new JButton(redAction)); panel.registerKeyboardAction(yellowAction, KeyStroke.getKeyStroke(KeyEvent.VK_Y, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(blueAction, KeyStroke.getKeyStroke(KeyEvent.VK_B, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); panel.registerKeyboardAction(redAction, KeyStroke.getKeyStroke(KeyEvent.VK_R, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); Container contentPane = frame.getContentPane(); contentPane.add(panel); JMenu m = new JMenu("Color"); m.add(yellowAction); m.add(blueAction); m.add(redAction); JMenuBar mbar = new JMenuBar(); mbar.add(m); frame.setJMenuBar(mbar); frame.show(); }
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()); }// www .j av 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.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: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()); }// w w w. ja va 2 s . c o m }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); actionMap.clear(); 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); }