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(String s) 

Source Link

Document

Parses a string and returns a KeyStroke.

Usage

From source file:ca.sfu.federation.action.CreateProjectAction.java

/**
 * CreateProjectAction default constructor.
 *///  ww  w.  ja  va2  s  . c  o m
public CreateProjectAction() {
    super("New Project", null);
    Icon icon = ImageIconUtils.loadIconById("file-new-project-icon");
    this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control shift N"));
    this.putValue(Action.LONG_DESCRIPTION, "Create a new project");
    this.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_N);
    this.putValue(Action.SHORT_DESCRIPTION, "Create a new project");
    this.putValue(Action.SMALL_ICON, icon);
}

From source file:SimpleMenu.java

/** The convenience method that creates menu panes */
public static JMenu create(ResourceBundle bundle, String menuname, String[] itemnames,
        ActionListener listener) {
    // Get the menu title from the bundle. Use name as default label.
    String menulabel;/*from   w w  w. j  a  v  a 2s  .  com*/
    try {
        menulabel = bundle.getString(menuname + ".label");
    } catch (MissingResourceException e) {
        menulabel = menuname;
    }

    // Create the menu pane.
    JMenu menu = new JMenu(menulabel);

    // For each named item in the menu.
    for (int i = 0; i < itemnames.length; i++) {
        // Look up the label for the item, using name as default.
        String itemlabel;
        try {
            itemlabel = bundle.getString(menuname + "." + itemnames[i] + ".label");
        } catch (MissingResourceException e) {
            itemlabel = itemnames[i];
        }

        JMenuItem item = new JMenuItem(itemlabel);

        // Look up an accelerator for the menu item
        try {
            String acceleratorText = bundle.getString(menuname + "." + itemnames[i] + ".accelerator");
            item.setAccelerator(KeyStroke.getKeyStroke(acceleratorText));
        } catch (MissingResourceException e) {
        }

        // Register an action listener and command for the item.
        if (listener != null) {
            item.addActionListener(listener);
            item.setActionCommand(itemnames[i]);
        }

        // Add the item to the menu.
        menu.add(item);
    }

    // Return the automatically created localized menu.
    return menu;
}

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

/**
 * BagInfoInputPane./*from w ww  .jav  a 2s  .  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:MenuTest.java

public MenuFrame() {
    setTitle("MenuTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    JMenu fileMenu = new JMenu("File");
    fileMenu.add(new TestAction("New"));

    // demonstrate accelerators

    JMenuItem openItem = fileMenu.add(new TestAction("Open"));
    openItem.setAccelerator(KeyStroke.getKeyStroke("ctrl O"));

    fileMenu.addSeparator();//from   w  w  w  . j  a  v a  2 s. c om

    saveAction = new TestAction("Save");
    JMenuItem saveItem = fileMenu.add(saveAction);
    saveItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));

    saveAsAction = new TestAction("Save As");
    fileMenu.add(saveAsAction);
    fileMenu.addSeparator();

    fileMenu.add(new AbstractAction("Exit") {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });

    // demonstrate check box and radio button menus

    readonlyItem = new JCheckBoxMenuItem("Read-only");
    readonlyItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            boolean saveOk = !readonlyItem.isSelected();
            saveAction.setEnabled(saveOk);
            saveAsAction.setEnabled(saveOk);
        }
    });

    ButtonGroup group = new ButtonGroup();

    JRadioButtonMenuItem insertItem = new JRadioButtonMenuItem("Insert");
    insertItem.setSelected(true);
    JRadioButtonMenuItem overtypeItem = new JRadioButtonMenuItem("Overtype");

    group.add(insertItem);
    group.add(overtypeItem);

    // demonstrate icons

    Action cutAction = new TestAction("Cut");
    cutAction.putValue(Action.SMALL_ICON, new ImageIcon("cut.gif"));
    Action copyAction = new TestAction("Copy");
    copyAction.putValue(Action.SMALL_ICON, new ImageIcon("copy.gif"));
    Action pasteAction = new TestAction("Paste");
    pasteAction.putValue(Action.SMALL_ICON, new ImageIcon("paste.gif"));

    JMenu editMenu = new JMenu("Edit");
    editMenu.add(cutAction);
    editMenu.add(copyAction);
    editMenu.add(pasteAction);

    // demonstrate nested menus

    JMenu optionMenu = new JMenu("Options");

    optionMenu.add(readonlyItem);
    optionMenu.addSeparator();
    optionMenu.add(insertItem);
    optionMenu.add(overtypeItem);

    editMenu.addSeparator();
    editMenu.add(optionMenu);

    // demonstrate mnemonics

    JMenu helpMenu = new JMenu("Help");
    helpMenu.setMnemonic('H');

    JMenuItem indexItem = new JMenuItem("Index");
    indexItem.setMnemonic('I');
    helpMenu.add(indexItem);

    // you can also add the mnemonic key to an action
    Action aboutAction = new TestAction("About");
    aboutAction.putValue(Action.MNEMONIC_KEY, new Integer('A'));
    helpMenu.add(aboutAction);

    // add all top-level menus to menu bar

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    menuBar.add(helpMenu);

    // demonstrate pop-ups

    popup = new JPopupMenu();
    popup.add(cutAction);
    popup.add(copyAction);
    popup.add(pasteAction);

    JPanel panel = new JPanel();
    panel.setComponentPopupMenu(popup);
    add(panel);

    // the following line is a workaround for bug 4966109
    panel.addMouseListener(new MouseAdapter() {
    });
}

From source file:com.croer.javaorange.diviner.SimpleOrangeTextPane.java

public SimpleOrangeTextPane() {
    CONFIGURATION = Configuration.getCONFIGURATION();

    //Create the style array to show the colors 
    List<Object> colorList = CONFIGURATION.getList("ColorWord");
    styles = new Style[colorList.size()];
    StyleContext sc = new StyleContext();
    for (int i = 0; i < colorList.size(); i++) {
        styles[i] = sc.addStyle((String) colorList.get(i), sc.getStyle(StyleContext.DEFAULT_STYLE));
        StyleConstants.setForeground(styles[i], ColorUtils.getColorByName((String) colorList.get(i)));
        StyleConstants.setBold(styles[i], true);
    }//from w  ww.j  a  v a  2s .c  o  m

    //Deactive key bindings 
    List<Object> navigationList = CONFIGURATION.getList("PageNavigation");
    for (Object object : navigationList) {
        getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(object.toString()), "none");
    }

    //Get the document for adding a document listener
    dsd = (DefaultStyledDocument) getDocument();
    dsd.addDocumentListener(new DocumentListenerTextPane());

    //...and setting a document filter
    documentFilter = new MyDocumentFilter();
    dsd.setDocumentFilter(documentFilter);
}

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);/*from w w w . java2 s  .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:FrameKey.java

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            setVisible(false);//from  w ww.j  a  va  2s .  co  m
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}

From source file:ca.sfu.federation.action.CreateProjectAction.java

/**
 * CreateProjectAction constructor.//ww  w .  jav a2 s .  com
 * @param Name Action name that will appear in menus.
 * @param MyIcon Action icon.
 * @param ToolTip Action description that will appear in Tool Tip.
 * @param MnemonicId Key mnemonic.
 */
public CreateProjectAction(String Name, Icon MyIcon, String ToolTip, Integer MnemonicId) {
    super(Name, MyIcon);
    this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control N"));
    this.putValue(Action.LONG_DESCRIPTION, ToolTip);
    this.putValue(Action.MNEMONIC_KEY, MnemonicId);
    this.putValue(Action.SHORT_DESCRIPTION, ToolTip);
    this.putValue(Action.SMALL_ICON, MyIcon);
}

From source file:inflor.core.plots.FCSChartPanel.java

public FCSChartPanel(JFreeChart chart, ChartSpec spec, FCSFrame data, TransformSet transforms) {
    super(chart);
    this.data = data;
    this.spec = spec;
    this.transformSet = transforms;

    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("DELETE"),
            DELETE_ANNOTATIONS_KEY);/*from ww  w  .  ja  v a 2  s  .  c om*/
    getActionMap().put(DELETE_ANNOTATIONS_KEY, new AbstractAction() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            deleteSelectedAnnotations();
        }
    });
    Range xRange = this.getChart().getXYPlot().getDomainAxis().getRange();
    xHandleSize = (xRange.getUpperBound() - xRange.getLowerBound()) / 100;
    Range yRange = this.getChart().getXYPlot().getDomainAxis().getRange();
    yHandleSize = (yRange.getUpperBound() - yRange.getLowerBound()) / 100;
}

From source file:daylightchart.gui.actions.ChartOptionsAction.java

/**
 * Shows Help-About.//  w  w  w . ja  v  a 2s . c o  m
 *
 * @param mainWindow
 *        Main window.
 */
public ChartOptionsAction(final DaylightChartGui mainWindow) {
    super(Messages.getString("DaylightChartGui.Menu.Options.ChartOptions"), //$NON-NLS-1$
            "/icons/chart_options.gif" //$NON-NLS-1$
    );
    setShortcutKey(KeyStroke.getKeyStroke("control alt C"));
    addActionListener(new GuiActionListener(mainWindow));
}