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 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()); }// ww w .j a v a2s .co m }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); Object[] keys = actionMap.keys(); 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()); }//from w ww .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(); Object[] keys = actionMap.allKeys(); 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()); }/*from w w w . j ava2 s . co m*/ }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); System.out.println(actionMap.size()); 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:KeyStrokeSample.java
public static void main(String args[]) { JFrame frame = new JFrame("KeyStroke Sample"); JButton buttonA = new JButton("<html><center>FOCUSED<br>control alt 7"); JButton buttonB = new JButton("<html><center>FOCUS/RELEASE<br>VK_ENTER"); JButton buttonC = new JButton("<html><center>ANCESTOR<br>VK_F4+SHIFT_MASK"); JButton buttonD = new JButton("<html><center>WINDOW<br>' '"); // Define ActionListener Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }// ww w . j a v a2s . c o 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); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(2, 2)); contentPane.add(buttonA); contentPane.add(buttonB); contentPane.add(buttonC); contentPane.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()); }/*from w w w .j av a 2 s. com*/ }; 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); 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 ww.ja 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(); Object[] keys = actionMap.keys(); for (Object key : keys) { actionMap.get(key); } 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()); }//from w w w . j a v a2 s .co m }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); Object[] keys = actionMap.keys(); for (Object key : keys) { actionMap.remove(key); } 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 ww .j av a 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 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: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);/*www . jav a 2 s . c om*/ 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);/* www . j a va2s . 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); }