Example usage for javax.swing JMenuItem addActionListener

List of usage examples for javax.swing JMenuItem addActionListener

Introduction

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

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:EditorPaneExample20.java

public static JMenu buildMenu(String name, MenuSpec[] menuSpecs, Hashtable actions) {
    int count = menuSpecs.length;

    JMenu menu = new JMenu(name);
    for (int i = 0; i < count; i++) {
        MenuSpec spec = menuSpecs[i];// w ww . j a  v  a  2s . c o  m
        if (spec.isSubMenu()) {
            // Recurse to handle a sub menu
            JMenu subMenu = buildMenu(spec.getName(), spec.getSubMenus(), actions);
            if (subMenu != null) {
                menu.add(subMenu);
            }
        } else if (spec.isAction()) {
            // It's an Action - add it directly to the menu
            menu.add(spec.getAction());
        } else {
            // It's an action name - add it if possible
            String actionName = spec.getActionName();
            Action targetAction = (Action) actions.get(actionName);

            // Create the menu item
            JMenuItem menuItem = menu.add(spec.getName());
            if (targetAction != null) {
                // The editor kit knows the action
                menuItem.addActionListener(targetAction);
            } else {
                // Action not known - disable the menu item
                menuItem.setEnabled(false);
            }
        }
    }

    // Return null if nothing was added to the menu.
    if (menu.getMenuComponentCount() == 0) {
        menu = null;
    }

    return menu;
}

From source file:ImageViewer.java

public ImageViewerFrame() {
    setTitle("ImageViewer");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    // use a label to display the images
    label = new JLabel();
    add(label);//from  ww  w  .  j ava  2  s .  c  o  m

    // set up the file chooser
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("."));

    // set up the menu bar
    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);

    JMenu menu = new JMenu("File");
    menuBar.add(menu);

    JMenuItem openItem = new JMenuItem("Open");
    menu.add(openItem);
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            // show file chooser dialog
            int result = chooser.showOpenDialog(null);

            // if file selected, set it as icon of the label
            if (result == JFileChooser.APPROVE_OPTION) {
                String name = chooser.getSelectedFile().getPath();
                label.setIcon(new ImageIcon(name));
            }
        }
    });

    JMenuItem exitItem = new JMenuItem("Exit");
    menu.add(exitItem);
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
}

From source file:MenuElementExample.java

public MenuElementExample() {

    popup = new JPopupMenu();
    slider = new SliderMenuItem();

    popup.add(slider);//from   www.j  a v a  2s  .  c  om
    popup.add(new JSeparator());

    JMenuItem ticks = new JCheckBoxMenuItem("Slider Tick Marks");
    ticks.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            slider.setPaintTicks(!slider.getPaintTicks());
        }
    });
    JMenuItem labels = new JCheckBoxMenuItem("Slider Labels");
    labels.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            slider.setPaintLabels(!slider.getPaintLabels());
        }
    });
    popup.add(ticks);
    popup.add(labels);
    popup.addPopupMenuListener(new PopupPrintListener());

    addMouseListener(new MousePopupListener());
}

From source file:com.codedx.burp.ContextMenuFactory.java

@Override
public List<JMenuItem> createMenuItems(IContextMenuInvocation invocation) {
    if (invocation.getInvocationContext() == IContextMenuInvocation.CONTEXT_SCANNER_RESULTS) {
        List<JMenuItem> lst = new ArrayList<JMenuItem>();
        JMenuItem export = new JMenuItem("Send to Code Dx");
        export.addActionListener(new ContextMenuActionListener(burpExtender, callbacks, invocation));
        lst.add(export);//from w w w .  j av a2 s.  c  o m
        return lst;
    }
    return null;
}

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.");
        }//w  w  w .  ja va2  s.  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:jmupen.MyListSelectionListener.java

@Override
public void mousePressed(MouseEvent e) {
    list.setSelectedIndex(list.locationToIndex(e.getPoint()));
    int index = list.getSelectedIndex();
    if (SwingUtilities.isRightMouseButton(e)) {
        JPopupMenu menu = new JPopupMenu();
        JMenuItem item = new JMenuItem("Remove");
        item.addActionListener(new ActionListener() {
            @Override//from w w w  .java2s.co  m
            public void actionPerformed(ActionEvent e) {
                if (index != -1) {
                    try {
                        System.out.println("Linea: " + index + " " + model.get(index));
                        model.removeElementAt(index);
                        removeLines(index, JMupenUtils.getRecents().toFile());
                        JMupenUtils.setGames(JMupenUtils.getGamesFromFile(JMupenUtils.getRecents()));
                    } catch (IOException ex) {
                        System.err.println("Error removing recent game. " + ex.getLocalizedMessage());
                    }
                }
            }
        });
        menu.add(item);
        menu.show(list, e.getX(), e.getY());
    }
}

From source file:at.tuwien.ifs.commons.gui.controls.MultiOptionToggleButton.java

public MultiOptionToggleButton(final ImageIcon[] icons, final String[] buttonTexts, final String tooltip,
        final MultiOptionToggleListener listener) {
    super(icons[0]);
    this.setToolTipText(tooltip);
    this.addActionListener(new ActionListener() {
        @Override//from ww  w . j a v a2s . co  m
        public void actionPerformed(ActionEvent e) {
            menu.show(MultiOptionToggleButton.this, 0, MultiOptionToggleButton.this.getHeight());

            // highlight current selection
            final Component[] components = menu.getComponents();
            for (Component c : components) {
                c.setBackground(null);
            }
            menu.getComponent(selectedIndex).setBackground(Color.GRAY);
        }
    });

    for (int i = 0; i < buttonTexts.length; i++) {
        JMenuItem jMenuItem = new JMenuItem(buttonTexts[i], icons[i]);
        jMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                listener.performAction(e.getActionCommand());
                selectedIndex = ArrayUtils.indexOf(buttonTexts, e.getActionCommand());
                setIcon(icons[selectedIndex]);
            }
        });
        menu.add(jMenuItem);
    }

    JMenuBar menuBar = new JMenuBar();
    menuBar.setBorderPainted(false);
    menuBar.add(menu);
}

From source file:Main.java

public Main() {
    popup = new JPopupMenu();
    ActionListener menuListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println("Popup menu item [" + event.getActionCommand() + "] was pressed.");
        }//from w  w  w.j av  a 2 s.  c  o  m
    };
    JMenuItem item;
    popup.add(item = new JMenuItem("Left"));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Center", new ImageIcon("center.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Right", new ImageIcon("right.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.add(item = new JMenuItem("Full", new ImageIcon("full.gif")));
    item.setHorizontalTextPosition(JMenuItem.RIGHT);
    item.addActionListener(menuListener);
    popup.addSeparator();
    popup.add(item = new JMenuItem("Settings . . ."));
    item.addActionListener(menuListener);

    popup.setLabel("Justification");
    popup.setBorder(new BevelBorder(BevelBorder.RAISED));
    popup.addPopupMenuListener(new PopupPrintListener());

    addMouseListener(new MousePopupListener());
}

From source file:com.bwc.ora.views.LabelPopupMenu.java

private void initMenu() {
    //determine if the point clicked already has an annotation
    boolean hasAnnotationAlready = hasAnnoationAlready();

    if (hasAnnotationAlready) {
        //add label to allow users to deselect the label for a given peak
        JMenuItem nonItem = new JMenuItem("Remove Label");
        nonItem.addActionListener(e -> {
            removeAnnotation();/*from   www.  j av  a 2 s  . com*/
            lrp.setAnnotations(LrpDisplayFrame.getInstance().getAnnotations());
        });
        add(nonItem);
    }

    //add list of possible labels for a point to the popup menu
    Arrays.stream(RetinalBand.values()).map(RetinalBand::toString).map(label -> {
        XYPointerAnnotation pointer = new XYPointerAnnotation(label,
                item.getDataset().getXValue(item.getSeriesIndex(), item.getItem()),
                item.getDataset().getYValue(item.getSeriesIndex(), item.getItem()), 0);
        pointer.setBaseRadius(35.0);
        pointer.setTipRadius(10.0);
        pointer.setFont(new Font("SansSerif", Font.PLAIN, 9));
        pointer.setPaint(Color.blue);
        pointer.setTextAnchor(TextAnchor.CENTER_LEFT);
        JMenuItem l = new JMenuItem(label);
        l.addActionListener(e -> {
            if (hasAnnotationAlready) {
                removeAnnotation();
            }
            chartPanel.getChart().getXYPlot().addAnnotation(pointer);
            lrp.setAnnotations(LrpDisplayFrame.getInstance().getAnnotations());
        });
        return l;
    }).forEach(this::add);
}

From source file:io.github.jeddict.relation.mapper.widget.table.SecondaryTableWidget.java

@Override
protected List<JMenuItem> getPopupMenuItemList() {
    List<JMenuItem> menuList = super.getPopupMenuItemList();
    JMenuItem menuItem = new JMenuItem("Delete Secondary Table");
    menuItem.addActionListener((ActionEvent e) -> {
        Entity entity = this.getBaseElementSpec().getEntity();
        entity.getAttributes().getAllAttribute().stream().filter(a -> a instanceof PersistenceBaseAttribute)
                .filter(a -> StringUtils.equalsIgnoreCase(((PersistenceBaseAttribute) a).getColumn().getTable(),
                        this.getBaseElementSpec().getSecondaryTable().getName()))
                .forEach(a -> ((PersistenceBaseAttribute) a).getColumn().setTable(null));
        entity.removeSecondaryTable(this.getBaseElementSpec().getSecondaryTable());
        ModelerFile parentFile = SecondaryTableWidget.this.getModelerScene().getModelerFile().getParentFile();
        DBUtil.openDBModeler(parentFile);
        JeddictLogger.recordDBAction("Delete Secondary Table");
    });/*from  w w w  .j ava2 s.  c o m*/
    menuList.add(0, menuItem);
    return menuList;
}