Example usage for javax.swing ActionMap put

List of usage examples for javax.swing ActionMap put

Introduction

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

Prototype

public void put(Object key, Action action) 

Source Link

Document

Adds a binding for key to action.

Usage

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--;/*from ww  w. j  av  a2 s .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:com.github.alexfalappa.nbspringboot.codegen.SpringDependencyDialog.java

/** Creates new form NewOkCancelDialog */
public SpringDependencyDialog(java.awt.Frame parent) {
    super(parent, true);
    initComponents();/*from w  w  w.j  a v a2 s .c om*/
    // 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: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);
        }/*from   www .j  a v  a 2 s.c o  m*/
    });

    // 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./*  w ww  .  j  av a2s.c  o m*/
 *
 * @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:TextAreaDemo.java

public TextAreaDemo() {
    super("TextAreaDemo");
    initComponents();/*from  w  w w  . jav  a  2  s  . c om*/

    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  2s .co  m

    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());
}

From source file:net.sf.maltcms.chromaui.jmztab.ui.validation.MzTabValidationElement.java

public MzTabValidationElement(Lookup lkp) {
    obj = lkp.lookup(MzTabDataObject.class);
    assert obj != null;
    initComponents();//from  w  w  w  .j  ava  2 s.co m
    view = new OutlineView();
    view.setQuickSearchAllowed(true);
    view.setTreeSortable(true);
    view.setPropertyColumns("message", "Message");
    view.getOutline().setRootVisible(false);
    add(view, BorderLayout.CENTER);
    ActionMap map = this.getActionMap();
    map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(manager));
    associateLookup(ExplorerUtils.createLookup(manager, map));
}

From source file:ListCutPaste.java

/**
 * Add the cut/copy/paste actions to the action map.
 */// w  w w . j  a v a  2  s  .  c  o  m
private void setMappings(JList list) {
    ActionMap map = list.getActionMap();
    map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());

}

From source file:org.pgptool.gui.ui.keyslist.KeysTableView.java

@SuppressWarnings("serial")
private void initTableKeyListener() {
    int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
    InputMap inputMap = table.getInputMap(condition);
    ActionMap actionMap = table.getActionMap();
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), DELETE);
    actionMap.put(DELETE, new AbstractAction() {
        @Override//from w  ww . j  ava2s  .c om
        public void actionPerformed(ActionEvent e) {
            if (pm == null) {
                return;
            }
            safePerformAction(pm.getActionDelete(), null);
        }
    });
}

From source file:DragPictureDemo2.java

public DTPicture(Image image) {
    super(image);
    addMouseMotionListener(this);

    //Add the cut/copy/paste key bindings to the input map.
    //Note that this step is redundant if you are installing
    //menu accelerators that cause these actions to be invoked.
    //DragPictureDemo does not use menu accelerators and, since
    //the default value of installInputMapBindings is true,
    //the bindings are installed. DragPictureDemo2 does use
    //menu accelerators and so calls setInstallInputMapBindings
    //with a value of false. Your program would do one or the
    //other, but not both.
    if (installInputMapBindings) {
        InputMap imap = this.getInputMap();
        imap.put(KeyStroke.getKeyStroke("ctrl X"), TransferHandler.getCutAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl C"), TransferHandler.getCopyAction().getValue(Action.NAME));
        imap.put(KeyStroke.getKeyStroke("ctrl V"), TransferHandler.getPasteAction().getValue(Action.NAME));
    }/*from  w w  w .ja  v  a 2s  .  c  om*/

    //Add the cut/copy/paste actions to the action map.
    //This step is necessary because the menu's action listener
    //looks for these actions to fire.
    ActionMap map = this.getActionMap();
    map.put(TransferHandler.getCutAction().getValue(Action.NAME), TransferHandler.getCutAction());
    map.put(TransferHandler.getCopyAction().getValue(Action.NAME), TransferHandler.getCopyAction());
    map.put(TransferHandler.getPasteAction().getValue(Action.NAME), TransferHandler.getPasteAction());
}