Example usage for javax.swing InputMap put

List of usage examples for javax.swing InputMap put

Introduction

In this page you can find the example usage for javax.swing InputMap put.

Prototype

public void put(KeyStroke keyStroke, Object actionMapKey) 

Source Link

Document

Adds a binding for keyStroke to actionMapKey .

Usage

From source file:ActionTest.java

public ActionFrame() {
    setTitle("ActionTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    buttonPanel = new JPanel();

    // define actions
    Action yellowAction = new ColorAction("Yellow", new ImageIcon("yellow-ball.gif"), Color.YELLOW);
    Action blueAction = new ColorAction("Blue", new ImageIcon("blue-ball.gif"), Color.BLUE);
    Action redAction = new ColorAction("Red", new ImageIcon("red-ball.gif"), Color.RED);

    // add buttons for these actions
    buttonPanel.add(new JButton(yellowAction));
    buttonPanel.add(new JButton(blueAction));
    buttonPanel.add(new JButton(redAction));

    // add panel to frame
    add(buttonPanel);//  w  w  w .  j  a v a2s.co  m

    // associate the Y, B, and R keys with names
    InputMap imap = buttonPanel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    imap.put(KeyStroke.getKeyStroke("ctrl Y"), "panel.yellow");
    imap.put(KeyStroke.getKeyStroke("ctrl B"), "panel.blue");
    imap.put(KeyStroke.getKeyStroke("ctrl R"), "panel.red");

    // associate the names with actions
    ActionMap amap = buttonPanel.getActionMap();
    amap.put("panel.yellow", yellowAction);
    amap.put("panel.blue", blueAction);
    amap.put("panel.red", redAction);
}

From source file:com.limegroup.gnutella.gui.GUIUtils.java

/**
 * Moves the action for the specified character from the 'ctrl' mask
 * to the 'meta' mask./*w w w .j a  v a 2 s .c  o m*/
 */
private static void replaceAction(InputMap map, char c) {
    KeyStroke ctrl = KeyStroke.getKeyStroke("control pressed " + c);
    KeyStroke meta = KeyStroke.getKeyStroke("meta pressed " + c);
    if (ctrl == null || meta == null)
        return;
    Object action = map.get(ctrl);
    if (action != null) {
        map.remove(ctrl);
        map.put(meta, action);
    }
}

From source file:edu.ku.brc.af.ui.forms.ResultSetController.java

/**
 * Sets the current (or focused ResultSetController) into the action btns.
 * @param rsc ResultSetController//from  w ww  .  j  av a2s.  c o m
 */
protected static void installRS(final ResultSetController rsc) {
    if (commandsHash == null && rsc != null) {
        rsc.createRSActions();
    }

    if (commandsHash != null) {
        for (RSAction<CommandType> rsca : commandsHash.values()) {
            rsca.setRs(rsc);

            if (rsc != null) {
                JButton btn = rsc.btnsHash.get(rsca.getType());
                if (btn != null) {
                    KeyStroke ks = UIHelper.getKeyStroke(rsca.getType());
                    String ACTION_KEY = rsca.getType().toString();
                    InputMap inputMap = btn.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
                    ActionMap actionMap = btn.getActionMap();

                    inputMap.put(ks, ACTION_KEY);
                    actionMap.put(ACTION_KEY, rsca);
                    rsca.setBtn(btn);
                } else {
                    //System.err.println("Btn for ["+rsca.getType()+"] is null");
                }
            }
        }
    }
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFrameActionsHolder.java

public void addAction(Action action, int index) {
    int oldIndex = findActionById(actionList, action.getId());
    if (oldIndex >= 0) {
        removeAction(actionList.get(oldIndex));
        if (index > oldIndex) {
            index--;//w w  w . j  a  v a  2s. co m
        }
    }

    if (action.getShortcutCombination() != null) {
        KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(action.getShortcutCombination());
        InputMap inputMap = panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        inputMap.put(keyStroke, action.getId());
        ActionMap actionMap = panel.getActionMap();
        actionMap.put(action.getId(), new ValidationAwareAction() {
            @Override
            public void actionPerformedAfterValidation(ActionEvent e) {
                action.actionPerform(component);
            }
        });
        shortcutActions.put(action, keyStroke);
    }

    actionList.add(index, action);
}

From source file:DigitalClock.java

public DigitalClock() {
    // Set default values for our properties
    setFormat(DateFormat.getTimeInstance(DateFormat.MEDIUM, getLocale()));
    setUpdateFrequency(1000); // Update once a second

    // Specify a Swing TransferHandler object to do the dirty work of
    // copy-and-paste and drag-and-drop for us. This one will transfer
    // the value of the "time" property. Since this property is read-only
    // it will allow drags but not drops.
    setTransferHandler(new TransferHandler("time"));

    // Since JLabel does not normally support drag-and-drop, we need an
    // event handler to detect a drag and start the transfer.
    addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            getTransferHandler().exportAsDrag(DigitalClock.this, e, TransferHandler.COPY);
        }//w  ww  .j  a v  a 2s  .  com
    });

    // Before we can have a keyboard binding for a Copy command,
    // the component needs to be able to accept keyboard focus.
    setFocusable(true);
    // Request focus when we're clicked on
    addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            requestFocus();
        }
    });
    // Use a LineBorder to indicate when we've got the keyboard focus
    addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            setBorder(LineBorder.createBlackLineBorder());
        }

        public void focusLost(FocusEvent e) {
            setBorder(null);
        }
    });

    // Now bind the Ctrl-C keystroke to a "Copy" command.
    InputMap im = new InputMap();
    im.setParent(getInputMap(WHEN_FOCUSED));
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_MASK), "Copy");
    setInputMap(WHEN_FOCUSED, im);

    // And bind the "Copy" command to a pre-defined Action that performs
    // a copy using the TransferHandler we've installed.
    ActionMap am = new ActionMap();
    am.setParent(getActionMap());
    am.put("Copy", TransferHandler.getCopyAction());
    setActionMap(am);

    // Create a javax.swing.Timer object that will generate ActionEvents
    // to tell us when to update the displayed time. Every updateFrequency
    // milliseconds, this timer will cause the actionPerformed() method
    // to be invoked. (For non-GUI applications, see java.util.Timer.)
    timer = new Timer(updateFrequency, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setText(getTime()); // set label to current time string
        }
    });
    timer.setInitialDelay(0); // Do the first update immediately
    timer.start(); // Start timing now!
}

From source file:cool.pandora.modeller.ui.BagInfoInputPane.java

/**
 * BagInfoInputPane./*from ww w .  ja v a2s . com*/
 *
 * @param bagView BagView
 */
public BagInfoInputPane(final BagView bagView) {
    this.bagView = bagView;
    this.defaultBag = bagView.getBag();
    populateForms(defaultBag);

    final InputMap im = this.getInputMap();
    im.put(KeyStroke.getKeyStroke("F2"), "tabNext");
    final ActionMap am = this.getActionMap();
    am.put("tabNext", new AbstractAction("tabNext") {
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent evt) {
            final int selected = getSelectedIndex();
            final int count = getComponentCount();
            if (selected >= 0 && selected < count - 1) {
                setSelectedIndex(selected + 1);
            } else {
                setSelectedIndex(0);
            }
            invalidate();
            repaint();
        }
    });
    this.setActionMap(am);
}

From source file:com.haulmont.cuba.desktop.theme.impl.DesktopThemeImpl.java

protected void initButtonsKeyBinding() {
    InputMap im = (InputMap) UIManager.get("Button.focusInputMap");
    im.put(KeyStroke.getKeyStroke("ENTER"), "pressed");
    im.put(KeyStroke.getKeyStroke("released ENTER"), "released");
}

From source file:com.github.alexfalappa.nbspringboot.codegen.SpringDependencyDialog.java

/** Creates new form NewOkCancelDialog */
public SpringDependencyDialog(java.awt.Frame parent) {
    super(parent, true);
    initComponents();//from www .j a  v a  2s  .c  o m
    // Close the dialog when Esc is pressed
    String cancelName = "cancel";
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
    ActionMap actionMap = rootPane.getActionMap();
    actionMap.put(cancelName, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            doClose(RET_CANCEL);
        }
    });
}

From source file:TextAreaDemo.java

public TextAreaDemo() {
    super("TextAreaDemo");
    initComponents();/*from  www .j  av  a 2 s.  c  o  m*/

    textArea.getDocument().addDocumentListener(this);

    InputMap im = textArea.getInputMap();
    ActionMap am = textArea.getActionMap();
    im.put(KeyStroke.getKeyStroke("ENTER"), COMMIT_ACTION);
    am.put(COMMIT_ACTION, new CommitAction());

    words = new ArrayList<String>(5);
    words.add("spark");
    words.add("special");
    words.add("spectacles");
    words.add("spectacular");
    words.add("swing");
}

From source file:TextFieldDemo.java

public TextFieldDemo() {
    initComponents();//from w ww .  jav a  2 s  . com

    InputStream in = getClass().getResourceAsStream("content.txt");
    try {
        textArea.read(new InputStreamReader(in), null);
    } catch (IOException e) {
        e.printStackTrace();
    }

    hilit = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(HILIT_COLOR);
    textArea.setHighlighter(hilit);

    entryBg = entry.getBackground();
    entry.getDocument().addDocumentListener(this);

    InputMap im = entry.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = entry.getActionMap();
    im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION);
    am.put(CANCEL_ACTION, new CancelAction());
}