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:de.tntinteractive.portalsammler.gui.DocumentTable.java

private void showPopup(final MouseEvent ev) {
    final JMenuItem open = new JMenuItem("Anzeigen");
    open.addActionListener(new ActionListener() {
        @Override//from  w  w  w .  j a  va  2  s. c om
        public void actionPerformed(final ActionEvent e) {
            DocumentTable.this.openSelectedRows();
        }
    });

    final JMenuItem export = new JMenuItem("Exportieren");
    export.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent e) {
            DocumentTable.this.exportSelectedRows();
        }
    });

    final JPopupMenu menu = new JPopupMenu();
    menu.add(open);
    menu.add(export);
    menu.show(ev.getComponent(), ev.getX(), ev.getY());
}

From source file:XMLWriteTest.java

public XMLWriteFrame() {
    setTitle("XMLWriteTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    chooser = new JFileChooser();

    // add component to frame

    comp = new RectangleComponent();
    add(comp);//  www  .j  av a 2 s.co  m

    // set up menu bar

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

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

    JMenuItem newItem = new JMenuItem("New");
    menu.add(newItem);
    newItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            comp.newDrawing();
        }
    });

    JMenuItem saveItem = new JMenuItem("Save with DOM/XSLT");
    menu.add(saveItem);
    saveItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                saveDocument();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(XMLWriteFrame.this, e.toString());
            }
        }
    });

    JMenuItem saveStAXItem = new JMenuItem("Save with StAX");
    menu.add(saveStAXItem);
    saveStAXItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            try {
                saveStAX();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(XMLWriteFrame.this, e.toString());
            }
        }
    });

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

From source file:fi.elfcloud.client.tree.ClusterNode.java

private void populatePopupMenu(JPopupMenu menu, BeaverGUI gui) {
    JMenuItem item;
    item = new JMenuItem(Messages.getString("ClusterNode.popup_modify")); //$NON-NLS-1$
    item.setActionCommand(Integer.toString(gui.ACTION_MODIFY));
    item.addActionListener(gui);
    item.setIcon(new ImageIcon(BeaverGUI.class.getResource("icons/modify_rename16.png"))); //$NON-NLS-1$
    menu.add(item);//from  ww w . ja  v  a  2  s.c o m

    item = new JMenuItem(Messages.getString("ClusterNode.popup_information")); //$NON-NLS-1$
    item.setIcon(new ImageIcon(BeaverGUI.class.getResource("icons/info_file16.png"))); //$NON-NLS-1$
    item.setActionCommand(Integer.toString(gui.ACTION_INFORMATION));
    item.addActionListener(gui);
    menu.add(item);
    menu.addSeparator();

    item = new JMenuItem(Messages.getString("ClusterNode.popup_delete")); //$NON-NLS-1$
    item.setActionCommand(Integer.toString(gui.ACTION_DELETE));
    item.addActionListener(gui);
    item.setIcon(new ImageIcon(BeaverGUI.class.getResource("icons/delete16.png"))); //$NON-NLS-1$
    menu.add(item);
}

From source file:net.pandoragames.far.ui.swing.component.UndoHistoryPopupMenu.java

private void init(SwingConfig config, ComponentRepository componentRepository) {
    // COPY//  www. j  a va 2 s .c  o m
    JMenuItem copy = new JMenuItem(config.getLocalizer().localize("label.copy"));
    copy.setAccelerator(KeyStroke.getKeyStroke("control C"));
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selection = textComponent.getSelectedText();
            if (selection != null) {
                StringSelection textTransfer = new StringSelection(selection);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(textTransfer, null);
            }
        }
    });
    this.add(copy);
    // PASTE
    JMenuItem paste = new JMenuItem(config.getLocalizer().localize("label.paste"));
    paste.setAccelerator(KeyStroke.getKeyStroke("control V"));
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            Transferable transfer = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (transfer != null && transfer.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String) transfer.getTransferData(DataFlavor.stringFlavor);
                    String selected = textComponent.getSelectedText();
                    if (selected == null) {
                        int insertAt = textComponent.getCaretPosition();
                        textComponent.getDocument().insertString(insertAt, text, null);
                    } else {
                        int start = textComponent.getSelectionStart();
                        int end = textComponent.getSelectionEnd();
                        textComponent.getDocument().remove(start, end - start);
                        textComponent.getDocument().insertString(start, text, null);
                    }
                }
            } catch (UnsupportedFlavorException e) {
                LogFactory.getLog(this.getClass()).error("UnsupportedFlavorException reading from clipboard",
                        e);
            } catch (IOException iox) {
                LogFactory.getLog(this.getClass()).error("IOException reading from clipboard", iox);
            } catch (BadLocationException blx) {
                LogFactory.getLog(this.getClass()).error("BadLocationException reading from clipboard", blx);
            }
        }
    });
    this.add(paste);
    // UNDO
    Action undoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_UNDO);
    if (undoAction != null) {
        undoAction.putValue(Action.NAME, config.getLocalizer().localize("label.undo"));
        this.add(undoAction);
    }
    // REDO
    Action redoAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_REDO);
    if (redoAction != null) {
        redoAction.putValue(Action.NAME, config.getLocalizer().localize("label.redo"));
        this.add(redoAction);
    }
    // PREVIOUS
    Action prevAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_PREVIOUS);
    if (prevAction != null) {
        prevAction.putValue(Action.NAME, config.getLocalizer().localize("label.previous"));
        this.add(prevAction);
    }
    // NEXT
    Action nextAction = textComponent.getActionMap().get(UndoHistory.ACTION_KEY_NEXT);
    if (nextAction != null) {
        nextAction.putValue(Action.NAME, config.getLocalizer().localize("label.next"));
        this.add(nextAction);
    }
}

From source file:net.sf.mzmine.modules.visualization.twod.TwoDPlot.java

TwoDPlot(RawDataFile rawDataFile, TwoDVisualizerWindow visualizer, TwoDDataSet dataset, Range rtRange,
        Range mzRange) {//from  w w w  . j a va  2 s  . c  o m

    super(null, true);

    this.rawDataFile = rawDataFile;
    this.rtRange = rtRange;
    this.mzRange = mzRange;

    setBackground(Color.white);
    setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));

    // set the X axis (retention time) properties
    xAxis = new NumberAxis("Retention time");
    xAxis.setAutoRangeIncludesZero(false);
    xAxis.setNumberFormatOverride(rtFormat);
    xAxis.setUpperMargin(0);
    xAxis.setLowerMargin(0);

    // set the Y axis (intensity) properties
    yAxis = new NumberAxis("m/z");
    yAxis.setAutoRangeIncludesZero(false);
    yAxis.setNumberFormatOverride(mzFormat);
    yAxis.setUpperMargin(0);
    yAxis.setLowerMargin(0);

    // set the plot properties
    plot = new TwoDXYPlot(dataset, rtRange, mzRange, xAxis, yAxis);
    plot.setBackgroundPaint(Color.white);
    plot.setDomainGridlinesVisible(false);
    plot.setRangeGridlinesVisible(false);

    // chart properties
    chart = new JFreeChart("", titleFont, plot, false);
    chart.setBackgroundPaint(Color.white);

    setChart(chart);

    // title
    chartTitle = chart.getTitle();
    chartTitle.setMargin(5, 0, 0, 0);
    chartTitle.setFont(titleFont);

    chartSubTitle = new TextTitle();
    chartSubTitle.setFont(subTitleFont);
    chartSubTitle.setMargin(5, 0, 0, 0);
    chart.addSubtitle(chartSubTitle);

    // disable maximum size (we don't want scaling)
    setMaximumDrawWidth(Integer.MAX_VALUE);
    setMaximumDrawHeight(Integer.MAX_VALUE);

    // set crosshair (selection) properties
    plot.setRangeCrosshairVisible(false);
    plot.setDomainCrosshairVisible(true);
    plot.setDomainCrosshairPaint(crossHairColor);
    plot.setDomainCrosshairStroke(crossHairStroke);

    // set rendering order
    plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    peakDataRenderer = new PeakDataRenderer();

    JMenuItem plotTypeMenuItem = new JMenuItem("Toggle centroid/continuous mode");
    plotTypeMenuItem.addActionListener(visualizer);
    plotTypeMenuItem.setActionCommand("SWITCH_PLOTMODE");
    add(plotTypeMenuItem);

    JPopupMenu popupMenu = getPopupMenu();
    popupMenu.addSeparator();
    popupMenu.add(plotTypeMenuItem);

}

From source file:de.peterspan.csv2db.AppWindow.java

private void initialize() {
    frame = new JFrame();

    frame.setIconImage(Icons.IC_APPLICATION_X_LARGE.getImage());

    AppWindow._frame = frame;/*from ww  w  .  j  a  v a  2 s .c o m*/
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            if (reallyExit() == JOptionPane.YES_OPTION) {
                SingleInstance.release();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            }
        }
    });

    frame.setTitle(Messages.getString("AppWindow.0") + " " + Messages.getString("AppWindow.version.code")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    frame.setLocationRelativeTo(null);
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);

    JMenu mnFile = new JMenu(Messages.getString("AppWindow.1")); //$NON-NLS-1$
    menuBar.add(mnFile);

    JMenuItem mntmExit = new JMenuItem(Messages.getString("AppWindow.3")); //$NON-NLS-1$
    mntmExit.setIcon(Icons.IC_LOGOUT_SMALL);
    mntmExit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            WindowEvent wev = new WindowEvent(frame, WindowEvent.WINDOW_CLOSING);
            Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);

        }
    });
    mnFile.add(mntmExit);

    JMenu mnEdit = new JMenu(Messages.getString("AppWindow.4")); //$NON-NLS-1$
    menuBar.add(mnEdit);

    JMenuItem mntmOptions = new JMenuItem(Messages.getString("AppWindow.5")); //$NON-NLS-1$
    mntmOptions.setIcon(Icons.IC_PREFERENCES_SYSTEM_SMALL);
    mntmOptions.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            //            new OptionsDialog(frame);
        }
    });
    mnEdit.add(mntmOptions);

    JMenu mnHelp = new JMenu(Messages.getString("AppWindow.6")); //$NON-NLS-1$
    menuBar.add(mnHelp);

    JMenuItem mntmAbout = new JMenuItem(Messages.getString("AppWindow.7")); //$NON-NLS-1$
    mntmAbout.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            try {
                new AboutDialog(frame);
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    });
    mnHelp.add(mntmAbout);

    createContent();

    DialogUtil.getInstance().setMainFrame(frame);

}

From source file:net.erdfelt.android.sdkfido.ui.SdkFidoFrame.java

private JMenu createFileMenu() {
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic('f');

    JMenuItem fileExit = new JMenuItem("Exit");
    fileExit.setMnemonic('x');
    fileExit.setActionCommand("exit");
    fileExit.addActionListener(actionMapper);
    fileMenu.add(fileExit);/*from w w  w. jav  a 2  s  .  com*/

    return fileMenu;
}

From source file:CutAndPasteDemo.java

public CutAndPasteDemo() {
    super("Cut And Paste Demonstration");

    clipboard = getToolkit().getSystemClipboard();

    GraphicsEnvironment.getLocalGraphicsEnvironment();
    Font font = new Font("LucidaSans", Font.PLAIN, 15);
    textArea1 = new JTextArea(davidMessage + andyMessage, 5, 25);
    textArea2 = new JTextArea("<Paste text here>", 5, 25);
    textArea1.setFont(font);/*from  ww  w  .j  ava  2 s  .co m*/
    textArea2.setFont(font);

    JPanel jPanel = new JPanel();
    JMenuBar jMenuBar = new JMenuBar();
    JMenuItem cutItem = new JMenuItem("Cut");
    JMenuItem pasteItem = new JMenuItem("Paste");
    JMenu jMenu = new JMenu("Edit");
    jMenu.add(cutItem);
    jMenu.add(pasteItem);

    cutItem.addActionListener(new CutActionListener());
    pasteItem.addActionListener(new PasteActionListener());

    jMenuBar.add(jMenu);
    jPanel.add(jMenuBar);

    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
    jPanel.add(textArea1);
    jPanel.add(Box.createRigidArea(new Dimension(0, 10)));
    jPanel.add(textArea2);

    getContentPane().add(jPanel, BorderLayout.CENTER);
}

From source file:components.PopupMenuDemo.java

public void createPopupMenu() {
    JMenuItem menuItem;

    //Create the popup menu.
    JPopupMenu popup = new JPopupMenu();
    menuItem = new JMenuItem("A popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);/*from   ww w .  j a  v a 2s  .c  o m*/
    menuItem = new JMenuItem("Another popup menu item");
    menuItem.addActionListener(this);
    popup.add(menuItem);

    //Add listener to the text area so the popup menu can come up.
    MouseListener popupListener = new PopupListener(popup);
    output.addMouseListener(popupListener);
}

From source file:fi.smaa.jsmaa.gui.SMAATRIGUIFactory.java

@Override
protected JMenuItem buildAddCriterionItem() {
    JMenuItem item = new JMenuItem("Add new");
    item.setIcon(ImageFactory.IMAGELOADER.getIcon(FileNames.ICON_ADDCRITERION));
    item.addActionListener(new AddOutrankingCriterionAction());
    return item;//from   ww w .  jav  a 2  s .  c o m
}