Java Code Examples for javax.swing.JComponent.getActionMap()

The following are Jave code examples for showing how to use getActionMap() of the javax.swing.JComponent class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: AddModulePanel.java   View Source Code Vote up 6 votes
private static void exchangeCommands(String[][] commandsToExchange,
        final JComponent target, final JComponent source) {
    InputMap targetBindings = target.getInputMap();
    KeyStroke[] targetBindingKeys = targetBindings.allKeys();
    ActionMap targetActions = target.getActionMap();
    InputMap sourceBindings = source.getInputMap();
    ActionMap sourceActions = source.getActionMap();
    for (int i = 0; i < commandsToExchange.length; i++) {
        String commandFrom = commandsToExchange[i][0];
        String commandTo = commandsToExchange[i][1];
        final Action orig = targetActions.get(commandTo);
        if (orig == null) {
            continue;
        }
        sourceActions.put(commandTo, new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                orig.actionPerformed(new ActionEvent(target, e.getID(), e.getActionCommand(), e.getWhen(), e.getModifiers()));
            }
        });
        for (int j = 0; j < targetBindingKeys.length; j++) {
            if (targetBindings.get(targetBindingKeys[j]).equals(commandFrom)) {
                sourceBindings.put(targetBindingKeys[j], commandTo);
            }
        }
    }
}
 
Example 2
Project: incubator-netbeans   File: ToolTipManagerEx.java   View Source Code Vote up 6 votes
/**
    * Registers a component for tooltip management.
    * <p>
    * This will register key bindings to show and hide the tooltip text
    * only if <code>component</code> has focus bindings. This is done
    * so that components that are not normally focus traversable, such
    * as <code>JLabel</code>, are not made focus traversable as a result
    * of invoking this method.
    *
    * @param component  a <code>JComponent</code> object to add
    * @see JComponent#isFocusTraversable
    */
   protected void registerComponent(JComponent component) {
       component.removeMouseListener(this);
       component.addMouseListener(this);
       component.removeMouseMotionListener(moveBeforeEnterListener);
component.addMouseMotionListener(moveBeforeEnterListener);

if (shouldRegisterBindings(component)) {
    // register our accessibility keybindings for this component
    // this will apply globally across L&F
    // Post Tip: Ctrl+F1
    // Unpost Tip: Esc and Ctrl+F1
    InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
    ActionMap actionMap = component.getActionMap();

    if (inputMap != null && actionMap != null) {
               //XXX remove
    }
}
   }
 
Example 3
Project: pdfjumbler   File: PdfJumbler.java   View Source Code Vote up 5 votes
private static void registerAccelerators(JComponent component, Action... actions) {
	InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
	ActionMap actionMap = component.getActionMap();
	for (Action action : actions) {
		inputMap.put((KeyStroke)action.getValue(Action.ACCELERATOR_KEY), action);
		actionMap.put(action, action);
	}
}
 
Example 4
Project: incubator-netbeans   File: ToolTipManagerEx.java   View Source Code Vote up 5 votes
/**
    * Removes a component from tooltip control.
    *
    * @param component  a <code>JComponent</code> object to remove
    */
   protected void unregisterComponent(JComponent component) {
       component.removeMouseListener(this);
component.removeMouseMotionListener(moveBeforeEnterListener);

if (shouldRegisterBindings(component)) {
    InputMap inputMap = component.getInputMap(JComponent.WHEN_FOCUSED);
    ActionMap actionMap = component.getActionMap();

    if (inputMap != null && actionMap != null) {
               //XXX remove
    }
}
   }
 
Example 5
Project: incubator-netbeans   File: Terminal.java   View Source Code Vote up 5 votes
private void setupKeymap(Set<Action> actions) {
JComponent comp = term.getScreen();

ActionMap actionMap = comp.getActionMap();
ActionMap newActionMap = new ActionMap();
newActionMap.setParent(actionMap);

printActionMap(actionMap);

InputMap inputMap = comp.getInputMap();
InputMap newInputMap = new InputMap();
newInputMap.setParent(inputMap);

Set<KeyStroke> passKeystrokes = new HashSet<KeyStroke>();

for (Action a : actions) {
    String name = (String) a.getValue(Action.NAME);
           KeyStroke accelerator = (KeyStroke) a.getValue(Action.ACCELERATOR_KEY);
    System.out.printf("Registering %s for %s\n", accelerator, name);
    if (accelerator == null)
	continue;
    newInputMap.put(accelerator, name);
    newActionMap.put(name, a);
    passKeystrokes.add(accelerator);
}

comp.setActionMap(newActionMap);
comp.setInputMap(JComponent.WHEN_FOCUSED, newInputMap);

       term.setKeyStrokeSet((HashSet) passKeystrokes);
   }
 
Example 6
Project: incubator-netbeans   File: DelegateActionMap.java   View Source Code Vote up 5 votes
@Override
public Action get(Object key) {
    ActionMap m;

    if (delegate == null) {
        JComponent comp = getComponent();
        if (comp == null) {
            m = null;
        } else {
            m = comp.getActionMap();
        }
    } else {
        m = delegate;
    }

    if (m != null) {
        Action a = m.get(key);

        if (a != null) {
            return a;
        }
    }

    Component owner = GlobalActionContextImpl.findFocusOwner();
    Action found = null;

    while ((owner != null) && (owner != getComponent())) {
        if ((found == null) && (owner instanceof JComponent)) {
            m = ((JComponent) owner).getActionMap();

            if (m != null) {
                found = m.get(key);
            }
        }

        owner = owner.getParent();
    }

    return (owner == getComponent()) ? found : null;
}
 
Example 7
Project: rapidminer   File: OperatorTransferHandler.java   View Source Code Vote up 5 votes
public static void addToActionMap(JComponent component) {
	ActionMap actionMap = component.getActionMap();
	actionMap.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
	actionMap.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
	actionMap.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
	actionMap.put(DeleteOperatorAction.getActionName(), new DeleteOperatorAction());

	// only required if you have not set the menu accelerators
	InputMap inputMap = component.getInputMap();
	inputMap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME));
	inputMap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME));
	inputMap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME));
	inputMap.put(KeyStroke.getKeyStroke("del"), DeleteOperatorAction.getActionName());
}
 
Example 8
Project: JavaGraph   File: Simulator.java   View Source Code Vote up 5 votes
/**
 * Adds the accelerator key for a given action to the action and input maps
 * of the simulator frame's content pane.
 * @param action the action to be added
 * @require <tt>frame.getContentPane()</tt> should be initialised
 */
public void addAccelerator(Action action) {
    JComponent contentPane = (JComponent) getFrame().getContentPane();
    ActionMap am = contentPane.getActionMap();
    am.put(action.getValue(Action.NAME), action);
    InputMap im = contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    im.put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), action.getValue(Action.NAME));
}
 
Example 9
Project: OpenJSharp   File: BasicLookAndFeel.java   View Source Code Vote up 5 votes
/**
 * Helper method to play a named sound.
 *
 * @param c JComponent to play the sound for.
 * @param actionKey Key for the sound.
 */
static void playSound(JComponent c, Object actionKey) {
    LookAndFeel laf = UIManager.getLookAndFeel();
    if (laf instanceof BasicLookAndFeel) {
        ActionMap map = c.getActionMap();
        if (map != null) {
            Action audioAction = map.get(actionKey);
            if (audioAction != null) {
                // pass off firing the Action to a utility method
                ((BasicLookAndFeel)laf).playSound(audioAction);
            }
        }
    }
}
 
Example 10
Project: jdk8u-jdk   File: BasicLookAndFeel.java   View Source Code Vote up 5 votes
/**
 * Helper method to play a named sound.
 *
 * @param c JComponent to play the sound for.
 * @param actionKey Key for the sound.
 */
static void playSound(JComponent c, Object actionKey) {
    LookAndFeel laf = UIManager.getLookAndFeel();
    if (laf instanceof BasicLookAndFeel) {
        ActionMap map = c.getActionMap();
        if (map != null) {
            Action audioAction = map.get(actionKey);
            if (audioAction != null) {
                // pass off firing the Action to a utility method
                ((BasicLookAndFeel)laf).playSound(audioAction);
            }
        }
    }
}
 
Example 11
Project: openjdk-jdk10   File: BasicLookAndFeel.java   View Source Code Vote up 5 votes
/**
 * Helper method to play a named sound.
 *
 * @param c JComponent to play the sound for.
 * @param actionKey Key for the sound.
 */
static void playSound(JComponent c, Object actionKey) {
    LookAndFeel laf = UIManager.getLookAndFeel();
    if (laf instanceof BasicLookAndFeel) {
        ActionMap map = c.getActionMap();
        if (map != null) {
            Action audioAction = map.get(actionKey);
            if (audioAction != null) {
                // pass off firing the Action to a utility method
                ((BasicLookAndFeel)laf).playSound(audioAction);
            }
        }
    }
}
 
Example 12
Project: MapAnalyst   File: GUIUtil.java   View Source Code Vote up 2 votes
/**
 * Adds support for plus and minus menu commands, typically for zooming in
 * and out. This is designed for menu items with key accelerators using
 * KeyEvent.VK_ADD and KeyEvent.VK_SUBTRACT (which are typically on the
 * numerical key pad). It adds support for KeyEvent.VK_MINUS and
 * KeyEvent.VK_PLUS, and KeyEvent.VK_EQUALS for keyboard layouts with the
 * plus sign as secondary key for the equals.
 *
 * @param zoomInAction action to call when the + key and the menu shortcut
 * key are pressed
 * @param zoomOutAction action to call when the - key and the menu shortcut
 * key are pressed
 * @param component component that will receive events and store actions
 */
public static void zoomMenuCommands(Action zoomInAction, Action zoomOutAction, JComponent component) {
    InputMap inputMap = component.getInputMap();
    ActionMap actionMap = component.getActionMap();
    zoomMenuCommands(inputMap, actionMap, zoomInAction, zoomOutAction);
}