Example usage for javax.swing KeyStroke getKeyStroke

List of usage examples for javax.swing KeyStroke getKeyStroke

Introduction

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

Prototype

public static KeyStroke getKeyStroke(int keyCode, int modifiers, boolean onKeyRelease) 

Source Link

Document

Returns a shared instance of a KeyStroke, given a numeric key code and a set of modifiers, specifying whether the key is activated when it is pressed or released.

Usage

From source file:com.mgmtp.perfload.loadprofiles.ui.util.SwingUtils.java

/**
 * Registers the enter key a {@link JButton}.
 * /*w  w  w.j a va 2 s.c  o  m*/
 * @param button
 *            The button
 */
public static void enterPressesWhenFocused(final JButton button) {
    button.registerKeyboardAction(
            button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), JComponent.WHEN_FOCUSED);

    button.registerKeyboardAction(
            button.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
            KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true), JComponent.WHEN_FOCUSED);
}

From source file:IntroExample.java

public IntroExample() {

    JMenu fileMenu = new JMenu("File");
    JMenu editMenu = new JMenu("Edit");
    JMenu otherMenu = new JMenu("Other");
    JMenu subMenu = new JMenu("SubMenu");
    JMenu subMenu2 = new JMenu("SubMenu2");

    //  Assemble the File menus with mnemonics
    ActionListener printListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Menu item [" + event.getActionCommand() + "] was pressed.");
        }//from  www  .  ja v a  2s.  c  o  m
    };
    for (int i = 0; i < fileItems.length; i++) {
        JMenuItem item = new JMenuItem(fileItems[i], fileShortcuts[i]);
        item.addActionListener(printListener);
        fileMenu.add(item);
    }

    //  Assemble the File menus with keyboard accelerators
    for (int i = 0; i < editItems.length; i++) {
        JMenuItem item = new JMenuItem(editItems[i]);
        item.setAccelerator(KeyStroke.getKeyStroke(editShortcuts[i],
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
        item.addActionListener(printListener);
        editMenu.add(item);
    }

    //  Insert a separator in the Edit Menu in Position 1 after "Undo"
    editMenu.insertSeparator(1);

    //  Assemble the submenus of the Other Menu
    JMenuItem item;
    subMenu2.add(item = new JMenuItem("Extra 2"));
    item.addActionListener(printListener);
    subMenu.add(item = new JMenuItem("Extra 1"));
    item.addActionListener(printListener);
    subMenu.add(subMenu2);

    //  Assemble the Other Menu itself
    otherMenu.add(subMenu);
    otherMenu.add(item = new JCheckBoxMenuItem("Check Me"));
    item.addActionListener(printListener);
    otherMenu.addSeparator();
    ButtonGroup buttonGroup = new ButtonGroup();
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 1"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.add(item = new JRadioButtonMenuItem("Radio 2"));
    item.addActionListener(printListener);
    buttonGroup.add(item);
    otherMenu.addSeparator();
    otherMenu.add(item = new JMenuItem("Potted Plant", new ImageIcon("image.gif")));
    item.addActionListener(printListener);

    //  Finally, add all the menus to the menu bar
    add(fileMenu);
    add(editMenu);
    add(otherMenu);
}

From source file:Main.java

/**
 * A helper for creating and updating key bindings for components with
 * mnemonics. The {@code pressed} action will be invoked when the mnemonic
 * is activated and the {@code released} action will be invoked when the
 * mnemonic is deactivated./*from ww  w.  j  a v  a 2  s  . c om*/
 * <p>
 * TODO establish an interface for the mnemonic properties, such as {@code
 * MnemonicEnabled} and change signature to {@code public static <T extends
 * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String
 * pressed, String released)}
 * 
 * @param c
 *            the component bindings to update
 * @param pressed
 *            the name of the action in the action map to invoke when the
 *            mnemonic is pressed
 * @param released
 *            the name of the action in the action map to invoke when the
 *            mnemonic is released (if the action is a toggle style, then
 *            this parameter should be {@code null})
 * @throws NullPointerException
 *             if the component is {@code null}
 */
public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
        Method mtd = clazz.getMethod("getMnemonic");
        m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
        if (map == null) {
            map = new ComponentInputMapUIResource(c);
            SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }

        map.clear();

        //TODO is ALT_MASK right for all platforms?
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
        map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
        if (map != null) {
            map.clear();
        }
    }
}

From source file:de.tor.tribes.ui.views.DSWorkbenchChurchFrame.java

/**
 * Creates new form DSWorkbenchChurchFrame
 *//* ww w  . ja v a  2 s  . c  o m*/
DSWorkbenchChurchFrame() {
    initComponents();
    centerPanel = new GenericTestPanel();
    jChurchPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jXPanel1);
    buildMenu();

    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if ("Delete".equals(e.getActionCommand())) {
                deleteSelection();
            } else if ("BBCopy".equals(e.getActionCommand())) {
                bbCopySelection();
            }
        }
    };
    capabilityInfoPanel1.addActionListener(listener);
    jChurchTable.registerKeyboardAction(listener, "Delete", delete,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jChurchTable.registerKeyboardAction(listener, "BBCopy", bbCopy,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    jChurchFrameAlwaysOnTop.setSelected(GlobalOptions.getProperties().getBoolean("church.frame.alwaysOnTop"));
    setAlwaysOnTop(jChurchFrameAlwaysOnTop.isSelected());

    jChurchTable.setModel(new ChurchTableModel());
    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.church_view",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>
    jChurchTable.getSelectionModel().addListSelectionListener(DSWorkbenchChurchFrame.this);
    pack();
}

From source file:de.tor.tribes.ui.views.DSWorkbenchWatchtowerFrame.java

/**
 * Creates new form DSWorkbenchWatchtowerFrame
 *///  w  w  w  .  j a  v a2 s .  co m
DSWorkbenchWatchtowerFrame() {
    initComponents();
    centerPanel = new GenericTestPanel();
    jWatchtowerPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jXPanel1);
    buildMenu();

    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);

    ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if ("Delete".equals(e.getActionCommand())) {
                deleteSelection();
            } else if ("BBCopy".equals(e.getActionCommand())) {
                bbCopySelection();
            }
        }
    };
    capabilityInfoPanel1.addActionListener(listener);
    jWatchtowerTable.registerKeyboardAction(listener, "Delete", delete,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jWatchtowerTable.registerKeyboardAction(listener, "BBCopy", bbCopy,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    jWatchtowerFrameAlwaysOnTop
            .setSelected(GlobalOptions.getProperties().getBoolean("watchtower.frame.alwaysOnTop"));
    setAlwaysOnTop(jWatchtowerFrameAlwaysOnTop.isSelected());

    jWatchtowerTable.setModel(new WatchtowerTableModel());
    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        //TODO create help page
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.church_view",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>
    jWatchtowerTable.getSelectionModel().addListSelectionListener(DSWorkbenchWatchtowerFrame.this);
    pack();
}

From source file:de.tor.tribes.ui.views.DSWorkbenchFormFrame.java

/** Creates new form DSWorkbenchFormFrame */
DSWorkbenchFormFrame() {/*from   w  w w. ja  v  a 2 s. c o  m*/
    initComponents();
    centerPanel = new GenericTestPanel();
    jFormPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jFormTablePanel);
    jFormsTable.setModel(new FormTableModel());
    jFormsTable.getSelectionModel().addListSelectionListener(DSWorkbenchFormFrame.this);
    buildMenu();

    KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);
    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    KeyStroke find = KeyStroke.getKeyStroke(KeyEvent.VK_F, ActionEvent.CTRL_MASK, false);
    capabilityInfoPanel1.addActionListener(this);
    jFormsTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DSWorkbenchFormFrame.getSingleton().actionPerformed(new ActionEvent(jFormsTable, 0, "Copy"));
        }
    }, "Copy", copy, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    jFormsTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DSWorkbenchFormFrame.getSingleton().actionPerformed(new ActionEvent(jFormsTable, 0, "BBCopy"));
        }
    }, "BBCopy", bbCopy, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    jFormsTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DSWorkbenchFormFrame.getSingleton().actionPerformed(new ActionEvent(jFormsTable, 0, "Delete"));
        }
    }, "Delete", delete, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    jFormsTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //no find
        }
    }, "Find", find, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.form_view",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>
}

From source file:de.tor.tribes.ui.views.DSWorkbenchDistanceFrame.java

/**
 * Creates new form DSWorkbenchDistanceFrame
 *//*from w w  w . j a  v a  2 s  . c  om*/
DSWorkbenchDistanceFrame() {
    initComponents();
    centerPanel = new GenericTestPanel(true);
    jDistancePanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jPanel2);
    unitBox = new JComboBox();
    unitBox.setRenderer(new UnitListCellRenderer());
    unitBox.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            cellRenderer.setUnit((UnitHolder) unitBox.getSelectedItem());
            jDistanceTable.repaint();
        }
    });
    buildMenu();
    jDistanceTable.setModel(new DistanceTableModel());
    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false);
    capabilityInfoPanel1.addActionListener(this);
    jDistanceTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DSWorkbenchDistanceFrame.getSingleton()
                    .actionPerformed(new ActionEvent(jDistanceTable, 0, "Delete"));
        }
    }, "Delete", delete, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jDistanceTable.registerKeyboardAction(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            DSWorkbenchDistanceFrame.getSingleton()
                    .actionPerformed(new ActionEvent(jDistanceTable, 0, "Paste"));
        }
    }, "Paste", paste, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jDistanceTable.getActionMap().put("find", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //disable find
        }
    });
    jDistanceTable.getSelectionModel().addListSelectionListener(DSWorkbenchDistanceFrame.this);
    cellRenderer.setUnit(UnknownUnit.getSingleton());

    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.distance_overview",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>
}

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

/**
 * Convert {@link KeyCombination} to {@link KeyStroke}.
 *
 * @param combination Key combination to convert
 * @return KeyStroke//from   w w  w  .  ja  va 2s .co m
 */
public static KeyStroke convertKeyCombination(KeyCombination combination) {
    KeyCombination.Modifier[] modifiers = combination.getModifiers();
    int modifiersMask = 0;
    if (modifiers != null && modifiers.length > 0) {
        for (KeyCombination.Modifier modifier : modifiers) {
            modifiersMask = modifiersMask | convertModifier(modifier);
        }
    }
    return KeyStroke.getKeyStroke(combination.getKey().getVirtualKey(), modifiersMask, false);
}

From source file:de.tor.tribes.ui.views.DSWorkbenchStatsFrame.java

DSWorkbenchStatsFrame() {
    initComponents();//from www .j  av  a 2  s.co m
    centerPanel = new GenericTestPanel();
    jStatsPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jMainStatPanel);
    buildMenu();
    capabilityInfoPanel1.addActionListener(this);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);
    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    jTabbedPane1.registerKeyboardAction(DSWorkbenchStatsFrame.this, "BBCopy", bbCopy,
            JComponent.WHEN_IN_FOCUSED_WINDOW);
    jTribeList.registerKeyboardAction(DSWorkbenchStatsFrame.this, "Delete", delete,
            JComponent.WHEN_IN_FOCUSED_WINDOW);

    jAlwaysOnTopBox.setSelected(GlobalOptions.getProperties().getBoolean("stats.frame.alwaysOnTop"));
    setAlwaysOnTop(jAlwaysOnTopBox.isSelected());

    jAllyList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            List allySelection = jAllyList.getSelectedValuesList();
            jTribeList.clearSelection();
            List<Tribe> tribes = new LinkedList<>();
            for (Object o : allySelection) {
                Tribe[] tribesForAlly = StatManager.getSingleton().getMonitoredTribes((Ally) o);
                for (Tribe t : tribesForAlly) {
                    if (!tribes.contains(t)) {
                        tribes.add(t);
                    }
                }
                Collections.sort(tribes);
                DefaultListModel<Tribe> model = new DefaultListModel<>();
                for (Tribe t : tribes) {
                    model.addElement(t);
                }
                jTribeList.setModel(model);
            }
        }
    });

    jTribeList.addListSelectionListener(new ListSelectionListener() {

        @Override
        public void valueChanged(ListSelectionEvent e) {
            fireUpdateChartEvent(null);
        }
    });

    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    jStartDate.setDate(c.getTime());
    jEndDate.setDate(c.getTime());
    jStatCreatePanel.setVisible(false);
    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.stats_view",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>

    pack();
}

From source file:de.tor.tribes.ui.views.DSWorkbenchSOSRequestAnalyzer.java

/**
 * Creates new form DSWorkbenchSOSRequestAnalyzer
 *///  w  w w  .  j  a  va2  s. com
DSWorkbenchSOSRequestAnalyzer() {
    initComponents();
    centerPanel = new GenericTestPanel(true);
    jSOSPanel.add(centerPanel, BorderLayout.CENTER);
    centerPanel.setChildComponent(jSOSInputPanel);
    buildMenu();
    jButton1.setIcon(new ImageIcon("./graphics/big/find.png"));
    capabilityInfoPanel1.addActionListener(DSWorkbenchSOSRequestAnalyzer.this);
    //  KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
    KeyStroke bbCopy = KeyStroke.getKeyStroke(KeyEvent.VK_B, ActionEvent.CTRL_MASK, false);
    KeyStroke delete = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0, false);
    //KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK, false);
    // jAttacksTable.registerKeyboardAction(DSWorkbenchSOSRequestAnalyzer.this, "Copy", copy, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jAttacksTable.registerKeyboardAction(DSWorkbenchSOSRequestAnalyzer.this, "BBCopy", bbCopy,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jAttacksTable.registerKeyboardAction(DSWorkbenchSOSRequestAnalyzer.this, "Delete", delete,
            JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    // jAttacksTable.registerKeyboardAction(DSWorkbenchSOSRequestAnalyzer.this, "Cut", cut, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    jAttacksTable.getActionMap().put("find", new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent e) {
            //ignore find
        }
    });

    jAttacksTable.setModel(new DefenseToolModel());
    jAttacksTable.setHighlighters(
            HighlighterFactory.createAlternateStriping(Constants.DS_ROW_A, Constants.DS_ROW_B));
    jAttacksTable.getColumnExt("Tendenz").setCellRenderer(new TendencyTableCellRenderer());
    jAttacksTable.getColumnExt("Status").setCellRenderer(new DefenseStatusTableCellRenderer());
    //jAttacksTable.getColumnExt("Wall").setCellRenderer(new WallLevellCellRenderer());
    jAttacksTable.getColumnExt("Verlustrate").setCellRenderer(new LossRatioTableCellRenderer());
    jAttacksTable.setDefaultRenderer(Date.class, new DateCellRenderer());
    jAttacksTable.setColumnControlVisible(false);
    jAttacksTable.setDefaultRenderer(Date.class, new DateCellRenderer());
    jAttacksTable.getTableHeader().setDefaultRenderer(new DefaultTableHeaderRenderer());
    jAttacksTable.requestFocus();

    jSupportsTable.setModel(new SupportsModel());
    jSupportsTable.setHighlighters(
            HighlighterFactory.createAlternateStriping(Constants.DS_ROW_A, Constants.DS_ROW_B));
    jSupportsTable.getTableHeader().setDefaultRenderer(new DefaultTableHeaderRenderer());
    jSupportsTable.setDefaultRenderer(Date.class, new ColoredDateCellRenderer());
    jSupportsTable.setDefaultRenderer(Boolean.class,
            new CustomBooleanRenderer(CustomBooleanRenderer.LayoutStyle.SENT_NOTSENT));

    new SupportCountdownThread().start();
    new SupportColorUpdateThread().start();
    jXInfoLabel.setLineWrap(true);

    // <editor-fold defaultstate="collapsed" desc=" Init HelpSystem ">
    if (!Constants.DEBUG) {
        GlobalOptions.getHelpBroker().enableHelpKey(getRootPane(), "pages.sos_analyzer",
                GlobalOptions.getHelpBroker().getHelpSet());
    }
    // </editor-fold>
}