List of usage examples for javax.swing JViewport getInputMap
public final InputMap getInputMap(int condition)
InputMap
that is used during condition
. 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 w w .j a v a 2 s.com*/ 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 www .java2s .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); }