Example usage for javax.swing ActionMap ActionMap

List of usage examples for javax.swing ActionMap ActionMap

Introduction

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

Prototype

public ActionMap() 

Source Link

Document

Creates an ActionMap with no parent and no mappings.

Usage

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from  w  w  w.  j ava2  s. co  m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();

    ActionMap newMap = new ActionMap();
    newMap.setParent(actionMap);

    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    String ACTION_KEY = "The Action";

    JFrame frame = new JFrame("KeyStroke Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton buttonA = new JButton("FOCUSED (control alt 7)");
    JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)");
    JButton buttonC = new JButton("ANCESTOR  (VK_F4+SHIFT_MASK)");
    JButton buttonD = new JButton("WINDOW (' ')");

    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            JButton source = (JButton) actionEvent.getSource();
            System.out.println("Activated: " + source.getText());
        }//from  w  w w  . j  ava  2s. c  o  m
    };

    KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7");
    InputMap inputMap = buttonA.getInputMap();
    inputMap.put(controlAlt7, ACTION_KEY);
    ActionMap actionMap = buttonA.getActionMap();

    ActionMap newMap = new ActionMap();
    newMap.setParent(actionMap);
    System.out.println(newMap.getParent());

    actionMap.put(ACTION_KEY, actionListener);

    KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
    inputMap = buttonB.getInputMap();
    inputMap.put(enter, ACTION_KEY);
    buttonB.setActionMap(actionMap);

    KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK);
    inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    inputMap.put(shiftF4, ACTION_KEY);
    buttonC.setActionMap(actionMap);

    KeyStroke space = KeyStroke.getKeyStroke(' ');
    inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(space, ACTION_KEY);
    buttonD.setActionMap(actionMap);

    frame.setLayout(new GridLayout(2, 2));
    frame.add(buttonA);
    frame.add(buttonB);
    frame.add(buttonC);
    frame.add(buttonD);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

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  w w. 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:ec.ui.chart.RevisionChartPanel.java

private JMenu buildMenu() {
    am = new ActionMap();
    am.put(COPY_ACTION, new CopyAction());

    im = new InputMap();
    KeyStrokes.putAll(im, KeyStrokes.COPY, COPY_ACTION);

    JMenu result = new JMenu();

    JMenuItem item;// w ww. j  av a 2s .c o  m

    item = new JMenuItem(am.get(COPY_ACTION));
    item.setText("Copy All");
    item.setAccelerator(KeyStrokes.COPY.get(0));
    ExtAction.hideWhenDisabled(item);
    result.add(item);

    return result;
}

From source file:ca.phon.app.session.editor.view.session_information.SessionInfoEditorView.java

private void init() {
    setLayout(new BorderLayout());

    contentPanel = new TierDataLayoutPanel();

    dateField = createDateField();//from w  ww .  j  a  v a2  s .  c o m
    dateField.getTextField().setColumns(10);
    dateField.setBackground(Color.white);

    mediaLocationField = new MediaSelectionField(getEditor().getProject());
    mediaLocationField.setEditor(getEditor());
    mediaLocationField.getTextField().setColumns(10);
    mediaLocationField.addPropertyChangeListener(FileSelectionField.FILE_PROP, mediaLocationListener);

    participantTable = new JXTable();
    participantTable.setVisibleRowCount(3);

    ComponentInputMap participantTableInputMap = new ComponentInputMap(participantTable);
    ActionMap participantTableActionMap = new ActionMap();

    ImageIcon deleteIcon = IconManager.getInstance().getIcon("actions/delete_user", IconSize.SMALL);
    final PhonUIAction deleteAction = new PhonUIAction(this, "deleteParticipant");
    deleteAction.putValue(PhonUIAction.SHORT_DESCRIPTION, "Delete selected participant");
    deleteAction.putValue(PhonUIAction.SMALL_ICON, deleteIcon);
    participantTableActionMap.put("DELETE_PARTICIPANT", deleteAction);
    participantTableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DELETE_PARTICIPANT");
    participantTableInputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "DELETE_PARTICIPANT");

    removeParticipantButton = new JButton(deleteAction);

    participantTable.setInputMap(WHEN_FOCUSED, participantTableInputMap);
    participantTable.setActionMap(participantTableActionMap);

    addParticipantButton = new JButton(new NewParticipantAction(getEditor(), this));
    addParticipantButton.setFocusable(false);

    ImageIcon editIcon = IconManager.getInstance().getIcon("actions/edit_user", IconSize.SMALL);
    final PhonUIAction editParticipantAct = new PhonUIAction(this, "editParticipant");
    editParticipantAct.putValue(PhonUIAction.NAME, "Edit participant...");
    editParticipantAct.putValue(PhonUIAction.SHORT_DESCRIPTION, "Edit selected participant...");
    editParticipantAct.putValue(PhonUIAction.SMALL_ICON, editIcon);
    editParticipantButton = new JButton(editParticipantAct);
    editParticipantButton.setFocusable(false);

    final CellConstraints cc = new CellConstraints();
    FormLayout participantLayout = new FormLayout("fill:pref:grow, pref, pref, pref", "pref, pref, pref:grow");
    JPanel participantPanel = new JPanel(participantLayout);
    participantPanel.setBackground(Color.white);
    participantPanel.add(new JScrollPane(participantTable), cc.xywh(1, 2, 3, 2));
    participantPanel.add(addParticipantButton, cc.xy(2, 1));
    participantPanel.add(editParticipantButton, cc.xy(3, 1));
    participantPanel.add(removeParticipantButton, cc.xy(4, 2));
    participantTable.addMouseListener(new MouseInputAdapter() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            if (arg0.getClickCount() == 2 && arg0.getButton() == MouseEvent.BUTTON1) {
                editParticipantAct.actionPerformed(new ActionEvent(arg0.getSource(), arg0.getID(), "edit"));
            }
        }

    });

    languageField = new LanguageField();
    languageField.getDocument().addDocumentListener(languageFieldListener);

    int rowIdx = 0;
    final JLabel dateLbl = new JLabel("Session Date");
    dateLbl.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(dateLbl, new TierDataConstraint(TierDataConstraint.TIER_LABEL_COLUMN, rowIdx));
    contentPanel.add(dateField, new TierDataConstraint(TierDataConstraint.FLAT_TIER_COLUMN, rowIdx++));

    final JLabel mediaLbl = new JLabel("Media");
    mediaLbl.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(mediaLbl, new TierDataConstraint(TierDataConstraint.TIER_LABEL_COLUMN, rowIdx));
    contentPanel.add(mediaLocationField, new TierDataConstraint(TierDataConstraint.FLAT_TIER_COLUMN, rowIdx++));

    final JLabel partLbl = new JLabel("Participants");
    partLbl.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(partLbl, new TierDataConstraint(TierDataConstraint.TIER_LABEL_COLUMN, rowIdx));
    contentPanel.add(participantPanel, new TierDataConstraint(TierDataConstraint.FLAT_TIER_COLUMN, rowIdx++));

    final JLabel langLbl = new JLabel("Language");
    langLbl.setHorizontalAlignment(SwingConstants.RIGHT);
    contentPanel.add(langLbl, new TierDataConstraint(TierDataConstraint.TIER_LABEL_COLUMN, rowIdx));
    contentPanel.add(languageField, new TierDataConstraint(TierDataConstraint.FLAT_TIER_COLUMN, rowIdx++));

    add(new JScrollPane(contentPanel), BorderLayout.CENTER);

    update();
}

From source file:org.docx4all.swing.text.WordMLEditorKit.java

private void initKeyBindings(JEditorPane editor) {
    ActionMap myActionMap = new ActionMap();
    InputMap myInputMap = new InputMap();

    KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.SHIFT_MASK);
    myActionMap.put(insertSoftBreakAction, new InsertSoftBreakAction(insertSoftBreakAction));
    myInputMap.put(ks, insertSoftBreakAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
    myActionMap.put(enterKeyTypedAction, new EnterKeyTypedAction(enterKeyTypedAction));
    myInputMap.put(ks, enterKeyTypedAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
    myActionMap.put(deleteNextCharAction, new DeleteNextCharAction(deleteNextCharAction));
    myInputMap.put(ks, deleteNextCharAction);

    ks = KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0);
    myActionMap.put(deletePrevCharAction, new DeletePrevCharAction(deletePrevCharAction));
    myInputMap.put(ks, deletePrevCharAction);

    myActionMap.setParent(editor.getActionMap());
    myInputMap.setParent(editor.getInputMap());
    editor.setActionMap(myActionMap);/*from   ww w  .j  ava  2s .  c  o m*/
    editor.setInputMap(JComponent.WHEN_FOCUSED, myInputMap);
}

From source file:org.gtdfree.GTDFree.java

private ActionMap getActionMap() {
    if (actionMap == null) {
        actionMap = new ActionMap();

        AbstractAction a = new AbstractAction(Messages.getString("GTDFree.View.Closed")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            @Override/*from   w  ww . j  a  v a  2 s.  c o  m*/
            public void actionPerformed(ActionEvent e) {
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_ALL_ACTIONS,
                        !getEngine().getGlobalProperties().getBoolean(GlobalProperties.SHOW_ALL_ACTIONS));
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.Closed.desc")); //$NON-NLS-1$
        actionMap.put(GlobalProperties.SHOW_ALL_ACTIONS, a);

        a = new AbstractAction(Messages.getString("GTDFree.View.Empty")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_EMPTY_FOLDERS, !getEngine()
                        .getGlobalProperties().getBoolean(GlobalProperties.SHOW_EMPTY_FOLDERS, true));
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.Empty.desc")); //$NON-NLS-1$
        actionMap.put(GlobalProperties.SHOW_EMPTY_FOLDERS, a);

        a = new AbstractAction(Messages.getString("GTDFree.View.ClosedLists")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_CLOSED_FOLDERS,
                        !getEngine().getGlobalProperties().getBoolean(GlobalProperties.SHOW_CLOSED_FOLDERS));
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.ClosedLists.desc")); //$NON-NLS-1$
        actionMap.put(GlobalProperties.SHOW_CLOSED_FOLDERS, a);

        a = new AbstractAction(Messages.getString("GTDFree.View.Overview")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            @Override
            public void actionPerformed(ActionEvent e) {
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_OVERVIEW_TAB,
                        !getEngine().getGlobalProperties().getBoolean(GlobalProperties.SHOW_OVERVIEW_TAB));
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.Overview.desc")); //$NON-NLS-1$
        actionMap.put(GlobalProperties.SHOW_OVERVIEW_TAB, a);

        a = new AbstractAction(Messages.getString("GTDFree.View.Quick")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_QUICK_COLLECT,
                        !getEngine().getGlobalProperties().getBoolean(GlobalProperties.SHOW_QUICK_COLLECT));
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.Quick.desc")); //$NON-NLS-1$
        actionMap.put(GlobalProperties.SHOW_QUICK_COLLECT, a);

        a = new AbstractAction(Messages.getString("GTDFree.View.Tray")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                boolean b = !getEngine().getGlobalProperties().getBoolean(GlobalProperties.SHOW_TRAY_ICON);
                getEngine().getGlobalProperties().putProperty(GlobalProperties.SHOW_TRAY_ICON, b);
                if (b) {
                    try {
                        SystemTray.getSystemTray().add(getTrayIcon());
                    } catch (AWTException e1) {
                        Logger.getLogger(this.getClass()).error("System tray icon initialization failed.", e1); //$NON-NLS-1$
                    }
                } else {
                    SystemTray.getSystemTray().remove(getTrayIcon());
                }
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.View.Tray.desc")); //$NON-NLS-1$
        a.setEnabled(SystemTray.isSupported());
        actionMap.put(GlobalProperties.SHOW_TRAY_ICON, a);

        a = new AbstractAction(Messages.getString("GTDFree.ImportExamples")) { //$NON-NLS-1$
            private static final long serialVersionUID = 1L;

            public void actionPerformed(ActionEvent e) {
                getImportDialog().getDialog(getJFrame()).setVisible(true);
            }
        };
        a.putValue(Action.SHORT_DESCRIPTION, Messages.getString("GTDFree.ImportExamples.desc")); //$NON-NLS-1$
        actionMap.put("importDialog", a); //$NON-NLS-1$
    }

    return actionMap;
}

From source file:org.objectstyle.cayenne.modeler.Application.java

protected void initActions() {
    // build action map
    actionMap = new ActionMap();

    registerAction(new ProjectAction(this));
    registerAction(new NewProjectAction(this)).setAlwaysOn(true);
    registerAction(new OpenProjectAction(this)).setAlwaysOn(true);
    registerAction(new ImportDataMapAction(this));
    registerAction(new SaveAction(this));
    registerAction(new SaveAsAction(this));
    registerAction(new RevertAction(this));
    registerAction(new ValidateAction(this));
    registerAction(new RemoveAction(this));
    registerAction(new CreateDomainAction(this));
    registerAction(new CreateNodeAction(this));
    registerAction(new CreateDataMapAction(this));
    registerAction(new GenerateClassesAction(this));
    registerAction(new CreateObjEntityAction(this));
    registerAction(new CreateDbEntityAction(this));
    registerAction(new CreateDerivedDbEntityAction(this));
    registerAction(new CreateProcedureAction(this));
    registerAction(new CreateQueryAction(this));
    registerAction(new CreateAttributeAction(this));
    registerAction(new CreateRelationshipAction(this));
    registerAction(new ObjEntitySyncAction(this));
    registerAction(new DerivedEntitySyncAction(this));
    registerAction(new ImportDBAction(this));
    registerAction(new ImportEOModelAction(this));
    registerAction(new GenerateDBAction(this));
    registerAction(new AboutAction(this)).setAlwaysOn(true);
    registerAction(new ConfigurePreferencesAction(this)).setAlwaysOn(true);
    registerAction(new ExitAction(this)).setAlwaysOn(true);
}

From source file:org.wings.SComponent.java

/**
 * Action map for key binding feature//  w  w  w  .ja v  a2s  . c  o  m
 *
 * @return The current action map
 * @see #setActionMap(javax.swing.ActionMap)
 */
public ActionMap getActionMap() {
    if (actionMap == null)
        actionMap = new ActionMap();
    return actionMap;
}