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) 

Source Link

Document

Returns a shared instance of a KeyStroke, given a numeric key code and a set of modifiers.

Usage

From source file:net.openbyte.gui.WorkFrame.java

public WorkFrame(ModificationAPI api, File workDirectory) {
    this.workDirectory = workDirectory;
    this.api = api;
    try {/*from   w  w w .ja v  a  2  s. c o m*/
        format = LibrarySerializer.deserialize(new File(workDirectory, "libraries.json"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    initComponents();
    KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK);
    menuItem4.setAccelerator(keyStroke);
    tree1.setModel(new FileSystemModel(this.workDirectory));
    JavaLanguageSupport support = new JavaLanguageSupport();
    try {
        support.getJarManager().addCurrentJreClassFileSource();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        for (LibraryEntry entry : format.libraries) {
            File entryJar = new File(entry.path);
            support.getJarManager().addClassFileSource(entryJar);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (api == ModificationAPI.BUKKIT) {
        try {
            support.getJarManager().addClassFileSource(new File(new File(workDirectory, "api"), "bukkit.jar"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    support.setAutoActivationDelay(0);
    support.setAutoCompleteEnabled(true);
    support.setParameterAssistanceEnabled(true);
    support.install(rSyntaxTextArea1);
    //PrintStream out = System.out;
    //System.setOut(new PrintStream(new StreamCapturer("OpenByte", this, out)));
}

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 w w w .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:com.santiagolizardo.madcommander.menu.FilesMenu.java

public FilesMenu(MainWindow mainWindow) {
    super(Translator.tr("Files"));
    setMnemonic(KeyEvent.VK_F);/*from w  w  w.  j  a  v  a2  s.  c o  m*/

    this.mainWindow = mainWindow;

    changeAttributesMenuItem = new LocalizedMenuItem("Change attributes...");
    changeAttributesMenuItem.addActionListener(this);

    changeModificationDatetimeMenuItem = new JMenuItem(Translator.tr("Change modification date and time..."));
    changeModificationDatetimeMenuItem.addActionListener(this);

    packMenuItem = new JMenuItem(new PackAction(mainWindow));
    unpackMenuItem = new LocalizedMenuItem("Unpack...");
    unpackMenuItem.addActionListener(this);
    compareByContent = new LocalizedMenuItem("Compare by content...");
    compareByContent.addActionListener(this);

    printMenuItem = new JMenuItem(Translator.tr("Print file list..."));
    printMenuItem.setIcon(IconFactory.newIcon("print.png"));
    printMenuItem.addActionListener(this);

    quitMenuItem = new LocalizedMenuItem("Quit");
    quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, KeyEvent.ALT_MASK));
    quitMenuItem.addActionListener(this);

    add(changeAttributesMenuItem);
    add(changeModificationDatetimeMenuItem);
    addSeparator();
    add(packMenuItem);
    add(unpackMenuItem);
    add(compareByContent);
    add(printMenuItem);
    addSeparator();
    add(quitMenuItem);
}

From source file:com.anrisoftware.globalpom.mnemonic.Accelerator.java

private KeyStroke getKeyCode(Integer keycode, String[] keynames) {
    int modifiers = 0;
    for (int i = 1; i < keynames.length; i++) {
        Integer mod = codeMap.getKeyCode(keynames[i]);
        log.checkKeyCode(mod, keynames);
        modifiers |= mod;//from w  w w.  ja v  a2 s  .co  m
    }
    return KeyStroke.getKeyStroke(keycode, modifiers);
}

From source file:components.IntegerEditor.java

public IntegerEditor(int min, int max) {
    super(new JFormattedTextField());
    ftf = (JFormattedTextField) getComponent();
    minimum = new Integer(min);
    maximum = new Integer(max);

    //Set up the editor for the integer cells.
    integerFormat = NumberFormat.getIntegerInstance();
    NumberFormatter intFormatter = new NumberFormatter(integerFormat);
    intFormatter.setFormat(integerFormat);
    intFormatter.setMinimum(minimum);//from   w w w.jav a  2  s.c  o  m
    intFormatter.setMaximum(maximum);

    ftf.setFormatterFactory(new DefaultFormatterFactory(intFormatter));
    ftf.setValue(minimum);
    ftf.setHorizontalAlignment(JTextField.TRAILING);
    ftf.setFocusLostBehavior(JFormattedTextField.PERSIST);

    //React when the user presses Enter while the editor is
    //active.  (Tab is handled as specified by
    //JFormattedTextField's focusLostBehavior property.)
    ftf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "check");
    ftf.getActionMap().put("check", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            if (!ftf.isEditValid()) { //The text is invalid.
                if (userSaysRevert()) { //reverted
                    ftf.postActionEvent(); //inform the editor
                }
            } else
                try { //The text is valid,
                    ftf.commitEdit(); //so use it.
                    ftf.postActionEvent(); //stop editing
                } catch (java.text.ParseException exc) {
                }
        }
    });
}

From source file:MenuDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;/*from  ww w .j  a va2s .  co  m*/
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("images/1.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    menuItem.addActionListener(this);
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    menuItem.addActionListener(this);
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    return menuBar;
}

From source file:components.TabComponentsDemo.java

private void initMenu() {
    JMenuBar menuBar = new JMenuBar();
    //create Options menu
    tabComponentsItem = new JCheckBoxMenuItem("Use TabComponents", true);
    tabComponentsItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.ALT_MASK));
    tabComponentsItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < pane.getTabCount(); i++) {
                if (tabComponentsItem.isSelected()) {
                    initTabComponent(i);
                } else {
                    pane.setTabComponentAt(i, null);
                }// ww  w .java  2 s  .co m
            }
        }
    });
    scrollLayoutItem = new JCheckBoxMenuItem("Set ScrollLayout");
    scrollLayoutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
    scrollLayoutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if (pane.getTabLayoutPolicy() == JTabbedPane.WRAP_TAB_LAYOUT) {
                pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
            } else {
                pane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
            }
        }
    });
    JMenuItem resetItem = new JMenuItem("Reset JTabbedPane");
    resetItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.ALT_MASK));
    resetItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            runTest();
        }
    });

    JMenu optionsMenu = new JMenu("Options");
    optionsMenu.add(tabComponentsItem);
    optionsMenu.add(scrollLayoutItem);
    optionsMenu.add(resetItem);
    menuBar.add(optionsMenu);
    setJMenuBar(menuBar);
}

From source file:MenuSelectionManagerDemo.java

public JMenuBar createMenuBar() {
    JMenuBar menuBar;//from ww w  . jav  a  2 s. c  o m
    JMenu menu, submenu;
    JMenuItem menuItem;
    JRadioButtonMenuItem rbMenuItem;
    JCheckBoxMenuItem cbMenuItem;

    //Create the menu bar.
    menuBar = new JMenuBar();

    //Build the first menu.
    menu = new JMenu("A Menu");
    menu.setMnemonic(KeyEvent.VK_A);
    menu.getAccessibleContext().setAccessibleDescription("The only menu in this program that has menu items");
    menuBar.add(menu);

    //a group of JMenuItems
    menuItem = new JMenuItem("A text-only menu item", KeyEvent.VK_T);
    //menuItem.setMnemonic(KeyEvent.VK_T); //used constructor instead
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
    menuItem.getAccessibleContext().setAccessibleDescription("This doesn't really do anything");
    menuItem.addActionListener(this);
    menu.add(menuItem);

    ImageIcon icon = createImageIcon("1.gif");
    menuItem = new JMenuItem("Both text and icon", icon);
    menuItem.setMnemonic(KeyEvent.VK_B);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    menuItem = new JMenuItem(icon);
    menuItem.setMnemonic(KeyEvent.VK_D);
    menuItem.addActionListener(this);
    menu.add(menuItem);

    //a group of radio button menu items
    menu.addSeparator();
    ButtonGroup group = new ButtonGroup();

    rbMenuItem = new JRadioButtonMenuItem("A radio button menu item");
    rbMenuItem.setSelected(true);
    rbMenuItem.setMnemonic(KeyEvent.VK_R);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    rbMenuItem = new JRadioButtonMenuItem("Another one");
    rbMenuItem.setMnemonic(KeyEvent.VK_O);
    group.add(rbMenuItem);
    rbMenuItem.addActionListener(this);
    menu.add(rbMenuItem);

    //a group of check box menu items
    menu.addSeparator();
    cbMenuItem = new JCheckBoxMenuItem("A check box menu item");
    cbMenuItem.setMnemonic(KeyEvent.VK_C);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    cbMenuItem = new JCheckBoxMenuItem("Another one");
    cbMenuItem.setMnemonic(KeyEvent.VK_H);
    cbMenuItem.addItemListener(this);
    menu.add(cbMenuItem);

    //a submenu
    menu.addSeparator();
    submenu = new JMenu("A submenu");
    submenu.setMnemonic(KeyEvent.VK_S);

    menuItem = new JMenuItem("An item in the submenu");
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
    menuItem.addActionListener(this);
    submenu.add(menuItem);

    menuItem = new JMenuItem("Another item");
    menuItem.addActionListener(this);
    submenu.add(menuItem);
    menu.add(submenu);

    //Build second menu in the menu bar.
    menu = new JMenu("Another Menu");
    menu.setMnemonic(KeyEvent.VK_N);
    menu.getAccessibleContext().setAccessibleDescription("This menu does nothing");
    menuBar.add(menu);

    Timer timer = new Timer(ONE_SECOND, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();
            for (int i = 0; i < path.length; i++) {
                if (path[i].getComponent() instanceof javax.swing.JMenuItem) {
                    JMenuItem mi = (JMenuItem) path[i].getComponent();
                    if ("".equals(mi.getText())) {
                        output.append("ICON-ONLY MENU ITEM > ");
                    } else {
                        output.append(mi.getText() + " > ");
                    }
                }
            }
            if (path.length > 0)
                output.append(newline);
        }
    });
    timer.start();
    return menuBar;
}

From source file:cz.muni.fi.javaseminar.kafa.bookregister.gui.MainWindow.java

private void initMainWindow() {
    this.addWindowFocusListener(new WindowAdapter() {
        @Override//ww w  . j a  v  a2  s. c  om
        public void windowGainedFocus(WindowEvent e) {
            updateModel();
        }
    });

    this.getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_DOWN_MASK),
            "NEW_AUTHOR");
    this.getRootPane().getActionMap().put("NEW_AUTHOR", spawnNewAuthorWindowAction);
}

From source file:net.sf.keystore_explorer.gui.dialogs.DCheckUpdate.java

private void initComponents() {
    jlCheckUpdate = new JLabel(res.getString("DCheckUpdate.jlCheckUpdate.text"));
    ImageIcon icon = new ImageIcon(getClass().getResource(res.getString("DCheckUpdate.jlCheckUpdate.image")));
    jlCheckUpdate.setIcon(icon);/*from  w  ww. j  a  va2  s.  c o  m*/
    jlCheckUpdate.setHorizontalTextPosition(SwingConstants.LEADING);
    jlCheckUpdate.setIconTextGap(15);

    jpCheckUpdate = new JPanel(new FlowLayout(FlowLayout.CENTER));
    jpCheckUpdate.add(jlCheckUpdate);
    jpCheckUpdate.setBorder(new EmptyBorder(5, 5, 5, 5));

    jpbCheckUpdate = new JProgressBar();
    jpbCheckUpdate.setIndeterminate(true);
    jpbCheckUpdate.setString("DCheckUpdate.jlCheckUpdate.text");

    jpProgress = new JPanel(new FlowLayout(FlowLayout.CENTER));
    jpProgress.add(jpbCheckUpdate);
    jpProgress.setBorder(new EmptyBorder(5, 5, 5, 5));

    jbCancel = new JButton(res.getString("DCheckUpdate.jbCancel.text"));
    jbCancel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });
    jbCancel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
            CANCEL_KEY);
    jbCancel.getActionMap().put(CANCEL_KEY, new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            cancelPressed();
        }
    });

    jpCancel = PlatformUtil.createDialogButtonPanel(jbCancel, false);

    getContentPane().add(jpCheckUpdate, BorderLayout.NORTH);
    getContentPane().add(jpProgress, BorderLayout.CENTER);
    getContentPane().add(jpCancel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent evt) {
            if ((checker != null) && (checker.isAlive())) {
                checker.interrupt();
            }
            closeDialog();
        }
    });

    setTitle(res.getString("DCheckUpdate.Title"));
    setResizable(false);

    pack();
}