Example usage for javax.swing JViewport getActionMap

List of usage examples for javax.swing JViewport getActionMap

Introduction

In this page you can find the example usage for javax.swing JViewport getActionMap.

Prototype

public final ActionMap getActionMap() 

Source Link

Document

Returns the ActionMap used to determine what Action to fire for particular KeyStroke binding.

Usage

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);/*from   w w  w  . j a v 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);//from   www.j a  v a2s  .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);
}