Example usage for javax.swing JRadioButtonMenuItem setAccelerator

List of usage examples for javax.swing JRadioButtonMenuItem setAccelerator

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The keystroke combination which will invoke the JMenuItem's actionlisteners without navigating the menu hierarchy")
public void setAccelerator(KeyStroke keyStroke) 

Source Link

Document

Sets the key combination which invokes the menu item's action listeners without navigating the menu hierarchy.

Usage

From source file:Main.java

public Main() {
    menuBar = new JMenuBar();
    JMenu justifyMenu = new JMenu("Justify");
    ActionListener actionPrinter = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0, "Action [" + e.getActionCommand() + "] performed!\n",
                        null);/* w  ww .  j  av  a  2 s .co m*/
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    JRadioButtonMenuItem leftJustify = new JRadioButtonMenuItem("Left", new ImageIcon("1.gif"));
    leftJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    leftJustify
            .setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    leftJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem rightJustify = new JRadioButtonMenuItem("Right", new ImageIcon("2.gif"));
    rightJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    rightJustify
            .setAccelerator(KeyStroke.getKeyStroke('R', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    rightJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem centerJustify = new JRadioButtonMenuItem("Center", new ImageIcon("3.gif"));
    centerJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    centerJustify
            .setAccelerator(KeyStroke.getKeyStroke('M', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    centerJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem fullJustify = new JRadioButtonMenuItem("Full", new ImageIcon("4.gif"));
    fullJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    fullJustify
            .setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    fullJustify.addActionListener(actionPrinter);

    ButtonGroup group = new ButtonGroup();
    group.add(leftJustify);
    group.add(rightJustify);
    group.add(centerJustify);
    group.add(fullJustify);

    justifyMenu.add(leftJustify);
    justifyMenu.add(rightJustify);
    justifyMenu.add(centerJustify);
    justifyMenu.add(fullJustify);

    menuBar.add(justifyMenu);
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
}

From source file:RadioButtonMenuItemExample.java

public RadioButtonMenuItemExample() {
    menuBar = new JMenuBar();
    JMenu justifyMenu = new JMenu("Justify");
    ActionListener actionPrinter = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                pane.getStyledDocument().insertString(0, "Action [" + e.getActionCommand() + "] performed!\n",
                        null);/*w  ww. j ava  2s. co m*/
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    };
    JRadioButtonMenuItem leftJustify = new JRadioButtonMenuItem("Left", new ImageIcon("1.gif"));
    leftJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    leftJustify
            .setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    leftJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem rightJustify = new JRadioButtonMenuItem("Right", new ImageIcon("2.gif"));
    rightJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    rightJustify
            .setAccelerator(KeyStroke.getKeyStroke('R', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    rightJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem centerJustify = new JRadioButtonMenuItem("Center", new ImageIcon("3.gif"));
    centerJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    centerJustify
            .setAccelerator(KeyStroke.getKeyStroke('M', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    centerJustify.addActionListener(actionPrinter);
    JRadioButtonMenuItem fullJustify = new JRadioButtonMenuItem("Full", new ImageIcon("4.gif"));
    fullJustify.setHorizontalTextPosition(JMenuItem.RIGHT);
    fullJustify
            .setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
    fullJustify.addActionListener(actionPrinter);

    ButtonGroup group = new ButtonGroup();
    group.add(leftJustify);
    group.add(rightJustify);
    group.add(centerJustify);
    group.add(fullJustify);

    justifyMenu.add(leftJustify);
    justifyMenu.add(rightJustify);
    justifyMenu.add(centerJustify);
    justifyMenu.add(fullJustify);

    menuBar.add(justifyMenu);
    menuBar.setBorder(new BevelBorder(BevelBorder.RAISED));
}

From source file:org.jab.docsearch.DocSearch.java

/**
 * Creates menu bar//from w  w w  . j av  a2 s . c  o  m
 *
 * @param menuBar  Menu bar
 */
private JMenuBar createMenuBar() {

    // menu bar
    JMenuBar menuBar = new JMenuBar();

    // ----- menu file

    // menu item print
    JMenuItem menuItemPrint = new JMenuItem(I18n.getString("menuitem.print"));
    menuItemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, Event.CTRL_MASK));
    menuItemPrint.setActionCommand("ac_print");
    menuItemPrint.addActionListener(this);

    // scale X
    JRadioButtonMenuItem scaleXRadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_width"));
    scaleXRadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, Event.CTRL_MASK));
    scaleXRadioBut.addActionListener(new ScaleXListener());
    // scale Y
    JRadioButtonMenuItem scaleYRadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_length"));
    scaleYRadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_L, Event.CTRL_MASK));
    scaleYRadioBut.addActionListener(new ScaleYListener());
    // scale fit
    JRadioButtonMenuItem scaleFitRadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_to_fit"));
    scaleFitRadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, Event.CTRL_MASK));
    scaleFitRadioBut.addActionListener(new ScaleFitListener());
    // scale half
    JRadioButtonMenuItem scaleHalfRadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_half"));
    scaleHalfRadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H, Event.CTRL_MASK));
    scaleHalfRadioBut.addActionListener(new ScaleHalfListener());
    // scale double
    JRadioButtonMenuItem scale2RadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_2x"));
    scale2RadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, Event.CTRL_MASK));
    scale2RadioBut.addActionListener(new Scale2Listener());
    // scale off
    JRadioButtonMenuItem scaleOffRadioBut = new JRadioButtonMenuItem(I18n.getString("print_scale_off"), true);
    scaleOffRadioBut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Event.CTRL_MASK));

    ButtonGroup scaleSetGroup = new ButtonGroup();
    scaleSetGroup.add(scale2RadioBut);
    scaleSetGroup.add(scaleFitRadioBut);
    scaleSetGroup.add(scaleHalfRadioBut);
    scaleSetGroup.add(scaleOffRadioBut);
    scaleSetGroup.add(scaleXRadioBut);
    scaleSetGroup.add(scaleYRadioBut);

    // build complete menu print preferences
    JMenu menuPrintPref = new JMenu(I18n.getString("menuitem.print_preferences"), true);
    menuPrintPref.add(scaleXRadioBut);
    menuPrintPref.add(scaleYRadioBut);
    menuPrintPref.add(scaleFitRadioBut);
    menuPrintPref.add(scaleHalfRadioBut);
    menuPrintPref.add(scale2RadioBut);
    menuPrintPref.addSeparator();
    menuPrintPref.add(scaleOffRadioBut);

    // menu item exit
    JMenuItem menuItemExit = new JMenuItem(I18n.getString("menuitem.exit"));
    menuItemExit.setActionCommand("ac_exit");
    menuItemExit.addActionListener(this);

    // build complete menu file
    JMenu menuFile = new JMenu(I18n.getString("menu.file"));
    menuFile.add(menuItemPrint);
    menuFile.add(menuPrintPref);
    menuFile.addSeparator();
    menuFile.add(menuItemExit);

    // add menu to menu bar
    menuBar.add(menuFile);

    // ----- menu index

    // menu item new
    JMenuItem menuItemNewIndex = new JMenuItem(I18n.getString("menuitem.new_index"));
    menuItemNewIndex.setActionCommand("ac_newindex");
    menuItemNewIndex.addActionListener(this);
    // menu item new spider
    JMenuItem menuItemNewSpiderIndex = new JMenuItem(I18n.getString("menuitem.new_spider_index"));
    menuItemNewSpiderIndex.setActionCommand("ac_newspiderindex");
    menuItemNewSpiderIndex.addActionListener(this);
    // menu item manage
    JMenuItem menuItemManageIndex = new JMenuItem(I18n.getString("menuitem.manage_indexes"));
    menuItemManageIndex.setActionCommand("ac_manageindex");
    menuItemManageIndex.addActionListener(this);
    // menu item rebuild
    JMenuItem menuItemRebuildIndex = new JMenuItem(I18n.getString("menuitem.rebuild_all_indexes"));
    menuItemRebuildIndex.setActionCommand("ac_rebuildindexes");
    menuItemRebuildIndex.addActionListener(this);
    // menu item import
    JMenuItem menuItemImportIndex = new JMenuItem(I18n.getString("menuitem.import_index"));
    menuItemImportIndex.setActionCommand("ac_importindex");
    menuItemImportIndex.addActionListener(this);

    // build complete menu index
    JMenu indexMenu = new JMenu(I18n.getString("menu.index"));
    indexMenu.add(menuItemNewIndex);
    indexMenu.add(menuItemNewSpiderIndex);
    indexMenu.add(menuItemManageIndex);
    indexMenu.add(menuItemRebuildIndex);
    indexMenu.addSeparator();
    indexMenu.add(menuItemImportIndex);

    // add menu to menu bar
    menuBar.add(indexMenu);

    // ----- menu bookmark

    // menu item add
    JMenuItem menuItemAddBookmark = new JMenuItem(I18n.getString("menuitem.add_bookmark"));
    menuItemAddBookmark.setActionCommand("ac_addbookmark");
    menuItemAddBookmark.addActionListener(this);
    // menu item delete all
    JMenuItem menuItemDeleteAll = new JMenuItem(I18n.getString("menuitem.delete_all_bookmarks"));
    menuItemDeleteAll.setActionCommand("ac_deleteallbookmarks");
    menuItemDeleteAll.addActionListener(this);

    // build complete menu index
    bookMarkMenu = new JMenu(I18n.getString("menu.bookmarks"));
    bookMarkMenu.add(menuItemAddBookmark);
    bookMarkMenu.add(menuItemDeleteAll);
    bookMarkMenu.addSeparator();

    // add menu to menu bar
    menuBar.add(bookMarkMenu);

    // ----- menu report

    // menu item metadata report
    JMenuItem menuItemMetadataReport = new JMenuItem(I18n.getString("menuitem.metadata_report"));
    menuItemMetadataReport.setActionCommand("ac_metadata_report");
    menuItemMetadataReport.addActionListener(this);
    // menu item servlet report
    JMenuItem menuItemServletReport = new JMenuItem(I18n.getString("menuitem.servlet_log_report"));
    menuItemServletReport.setActionCommand("ac_servlet_log_report");
    menuItemServletReport.addActionListener(this);

    // build complete menu report
    JMenu reportMenu = new JMenu(I18n.getString("menu.reports"));
    reportMenu.add(menuItemMetadataReport);
    reportMenu.add(menuItemServletReport);

    // add menu to menu bar
    menuBar.add(reportMenu);

    // ----- menu tools

    // menu item makr cd
    JMenuItem menuItemMakeCD = new JMenuItem(I18n.getString("menuitem.make_cd"));
    menuItemMakeCD.setActionCommand("ac_makecd");
    menuItemMakeCD.addActionListener(this);
    // menu item setting
    JMenuItem menuItemSetting = new JMenuItem(I18n.getString("menuitem.settings"));
    menuItemSetting.setActionCommand("ac_settings");
    menuItemSetting.addActionListener(this);

    // build complete menu report
    JMenu menuTool = new JMenu(I18n.getString("menu.tools"));
    menuTool.add(menuItemMakeCD);
    menuTool.addSeparator();
    menuTool.add(menuItemSetting);

    // add menu to menu bar
    menuBar.add(menuTool);

    // ----- menu help

    // menu item search tip
    JMenuItem menuItemSearchTip = new JMenuItem(I18n.getString("menuitem.search_tips"));
    menuItemSearchTip.setActionCommand("ac_search_tips");
    menuItemSearchTip.addActionListener(this);
    // menu item about
    JMenuItem menuItemAbout = new JMenuItem(I18n.getString("menuitem.about"));
    menuItemAbout.setActionCommand("ac_about");
    menuItemAbout.addActionListener(this);

    // build complete menu help
    JMenu menuHelp = new JMenu(I18n.getString("menu.help"));
    menuHelp.add(menuItemSearchTip);
    menuHelp.add(menuItemAbout);

    // add menu to menu bar
    menuBar.add(menuHelp);

    // finished
    return menuBar;
}