Example usage for javax.swing JRadioButtonMenuItem addChangeListener

List of usage examples for javax.swing JRadioButtonMenuItem addChangeListener

Introduction

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

Prototype

public void addChangeListener(ChangeListener l) 

Source Link

Document

Adds a ChangeListener to the button.

Usage

From source file:MenuX.java

public static void main(String args[]) {
    ActionListener actionListener = new MenuActionListener();
    MenuKeyListener menuKeyListener = new MyMenuKeyListener();
    ChangeListener cListener = new MyChangeListener();
    MenuListener menuListener = new MyMenuListener();
    MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    manager.addChangeListener(cListener);
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new VerticalMenuBar();
    //    JMenuBar bar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    file.addChangeListener(cListener);// w w w  .j a  v a 2 s  .c  o m
    file.addMenuListener(menuListener);
    file.addMenuKeyListener(menuKeyListener);
    JPopupMenu popupMenu = file.getPopupMenu();
    popupMenu.setLayout(new GridLayout(3, 3));
    bar.add(file);

    // File->New, N - Mnemonic
    JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N);
    newItem.addActionListener(actionListener);
    newItem.addChangeListener(cListener);
    newItem.addMenuKeyListener(menuKeyListener);
    file.add(newItem);

    // File->Open, O - Mnemonic
    JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O);
    openItem.addActionListener(actionListener);
    openItem.addChangeListener(cListener);
    openItem.addMenuKeyListener(menuKeyListener);
    file.add(openItem);

    // File->Close, C - Mnemonic
    JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeItem.addActionListener(actionListener);
    closeItem.addChangeListener(cListener);
    closeItem.addMenuKeyListener(menuKeyListener);
    file.add(closeItem);

    // Separator
    file.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveItem.addActionListener(actionListener);
    saveItem.addChangeListener(cListener);
    saveItem.addMenuKeyListener(menuKeyListener);
    file.add(saveItem);

    // Separator
    file.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitItem.addActionListener(actionListener);
    exitItem.addChangeListener(cListener);
    exitItem.addMenuKeyListener(menuKeyListener);
    file.add(exitItem);

    // Edit Menu, E - Mnemonic
    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    edit.addChangeListener(cListener);
    edit.addMenuListener(menuListener);
    edit.addMenuKeyListener(menuKeyListener);
    bar.add(edit);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutItem.addActionListener(actionListener);
    cutItem.addChangeListener(cListener);
    cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    cutItem.addMenuKeyListener(menuKeyListener);
    edit.add(cutItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyItem.addActionListener(actionListener);
    copyItem.addChangeListener(cListener);
    copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    copyItem.addMenuKeyListener(menuKeyListener);
    copyItem.setEnabled(false);
    edit.add(copyItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteItem.addActionListener(actionListener);
    pasteItem.addChangeListener(cListener);
    pasteItem.addMenuKeyListener(menuKeyListener);
    pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);

    // Separator
    edit.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F);
    findItem.addActionListener(actionListener);
    findItem.addChangeListener(cListener);
    findItem.addMenuKeyListener(menuKeyListener);
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    JMenu findOptions = new JMenu("Options");
    findOptions.addChangeListener(cListener);
    findOptions.addMenuListener(menuListener);
    findOptions.addMenuKeyListener(menuKeyListener);
    Icon atIcon = new ImageIcon("at.gif");
    findOptions.setIcon(atIcon);
    findOptions.setMnemonic(KeyEvent.VK_O);

    // ButtonGrou for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    forward.addActionListener(actionListener);
    forward.addChangeListener(cListener);
    forward.addMenuKeyListener(menuKeyListener);
    forward.setMnemonic(KeyEvent.VK_F);
    findOptions.add(forward);
    directionGroup.add(forward);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    backward.addActionListener(actionListener);
    backward.addChangeListener(cListener);
    backward.addMenuKeyListener(menuKeyListener);
    backward.setMnemonic(KeyEvent.VK_B);
    findOptions.add(backward);
    directionGroup.add(backward);

    // Separator
    findOptions.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive");
    caseItem.addActionListener(actionListener);
    caseItem.addChangeListener(cListener);
    caseItem.addMenuKeyListener(menuKeyListener);
    caseItem.setMnemonic(KeyEvent.VK_C);
    findOptions.add(caseItem);
    edit.add(findOptions);

    frame.setJMenuBar(bar);
    //    frame.getContentPane().add(bar, BorderLayout.EAST);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:MenuY.java

public static void main(String args[]) {
    ActionListener actionListener = new MenuActionListener();
    MenuKeyListener menuKeyListener = new MyMenuKeyListener();
    ChangeListener cListener = new MyChangeListener();
    MenuListener menuListener = new MyMenuListener();
    MenuSelectionManager manager = MenuSelectionManager.defaultManager();
    manager.addChangeListener(cListener);
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar bar = new VerticalMenuBar();
    //    JMenuBar bar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    file.addChangeListener(cListener);//  www . j a  v  a  2 s.co  m
    file.addMenuListener(menuListener);
    file.addMenuKeyListener(menuKeyListener);
    JPopupMenu popupMenu = file.getPopupMenu();
    popupMenu.setLayout(new GridLayout(3, 3));
    bar.add(file);

    // File->New, N - Mnemonic
    JMenuItem newItem = new JMenuItem("New", KeyEvent.VK_N);
    newItem.addActionListener(actionListener);
    newItem.addChangeListener(cListener);
    newItem.addMenuKeyListener(menuKeyListener);
    file.add(newItem);

    // File->Open, O - Mnemonic
    JMenuItem openItem = new JMenuItem("Open", KeyEvent.VK_O);
    openItem.addActionListener(actionListener);
    openItem.addChangeListener(cListener);
    openItem.addMenuKeyListener(menuKeyListener);
    file.add(openItem);

    // File->Close, C - Mnemonic
    JMenuItem closeItem = new JMenuItem("Close", KeyEvent.VK_C);
    closeItem.addActionListener(actionListener);
    closeItem.addChangeListener(cListener);
    closeItem.addMenuKeyListener(menuKeyListener);
    file.add(closeItem);

    // Separator
    file.addSeparator();

    // File->Save, S - Mnemonic
    JMenuItem saveItem = new JMenuItem("Save", KeyEvent.VK_S);
    saveItem.addActionListener(actionListener);
    saveItem.addChangeListener(cListener);
    saveItem.addMenuKeyListener(menuKeyListener);
    file.add(saveItem);

    // Separator
    file.addSeparator();

    // File->Exit, X - Mnemonic
    JMenuItem exitItem = new JMenuItem("Exit", KeyEvent.VK_X);
    exitItem.addActionListener(actionListener);
    exitItem.addChangeListener(cListener);
    exitItem.addMenuKeyListener(menuKeyListener);
    file.add(exitItem);

    // Edit Menu, E - Mnemonic
    JMenu edit = new JMenu("Edit");
    edit.setMnemonic(KeyEvent.VK_E);
    edit.addChangeListener(cListener);
    edit.addMenuListener(menuListener);
    edit.addMenuKeyListener(menuKeyListener);
    bar.add(edit);

    // Edit->Cut, T - Mnemonic, CTRL-X - Accelerator
    JMenuItem cutItem = new JMenuItem("Cut", KeyEvent.VK_T);
    cutItem.addActionListener(actionListener);
    cutItem.addChangeListener(cListener);
    cutItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK));
    cutItem.addMenuKeyListener(menuKeyListener);
    edit.add(cutItem);

    // Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
    JMenuItem copyItem = new JMenuItem("Copy", KeyEvent.VK_C);
    copyItem.addActionListener(actionListener);
    copyItem.addChangeListener(cListener);
    copyItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Event.CTRL_MASK));
    copyItem.addMenuKeyListener(menuKeyListener);
    copyItem.setEnabled(false);
    edit.add(copyItem);

    // Edit->Paste, P - Mnemonic, CTRL-V - Accelerator, Disabled
    JMenuItem pasteItem = new JMenuItem("Paste", KeyEvent.VK_P);
    pasteItem.addActionListener(actionListener);
    pasteItem.addChangeListener(cListener);
    pasteItem.addMenuKeyListener(menuKeyListener);
    pasteItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);

    // Separator
    edit.addSeparator();

    // Edit->Find, F - Mnemonic, F3 - Accelerator
    JMenuItem findItem = new JMenuItem("Find", KeyEvent.VK_F);
    findItem.addActionListener(actionListener);
    findItem.addChangeListener(cListener);
    findItem.addMenuKeyListener(menuKeyListener);
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);

    // Edit->Options Submenu, O - Mnemonic, at.gif - Icon Image File
    Icon atIcon = new ImageIcon("at.gif");
    Action findAction = new AbstractAction("Options", atIcon) {
        ActionListener actionListener = new MenuActionListener();

        public void actionPerformed(ActionEvent e) {
            actionListener.actionPerformed(e);
        }
    };
    findAction.putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_O));
    JMenuItem jMenuItem = new JMenuItem(findAction);

    JMenu findOptions = new JMenu(findAction);
    findOptions.addChangeListener(cListener);
    findOptions.addMenuListener(menuListener);
    findOptions.addMenuKeyListener(menuKeyListener);

    // ButtonGrou for radio buttons
    ButtonGroup directionGroup = new ButtonGroup();

    // Edit->Options->Forward, F - Mnemonic, in group
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    forward.addActionListener(actionListener);
    forward.addChangeListener(cListener);
    forward.addMenuKeyListener(menuKeyListener);
    forward.setMnemonic(KeyEvent.VK_F);
    findOptions.add(forward);
    directionGroup.add(forward);

    // Edit->Options->Backward, B - Mnemonic, in group
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    backward.addActionListener(actionListener);
    backward.addChangeListener(cListener);
    backward.addMenuKeyListener(menuKeyListener);
    backward.setMnemonic(KeyEvent.VK_B);
    findOptions.add(backward);
    directionGroup.add(backward);

    // Separator
    findOptions.addSeparator();

    // Edit->Options->Case Sensitive, C - Mnemonic
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Sensitive");
    caseItem.addActionListener(actionListener);
    caseItem.addChangeListener(cListener);
    caseItem.addMenuKeyListener(menuKeyListener);
    caseItem.setMnemonic(KeyEvent.VK_C);
    findOptions.add(caseItem);
    edit.add(findOptions);

    frame.setJMenuBar(bar);
    //    frame.getContentPane().add(bar, BorderLayout.EAST);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:org.cds06.speleograph.graph.SeriesMenu.java

private JPopupMenu createPopupMenuForSeries(final Series series) {

    if (series == null)
        return new JPopupMenu();

    final JPopupMenu menu = new JPopupMenu(series.getName());

    menu.removeAll();//from   ww w.  ja v  a2s  .co  m

    menu.add(new AbstractAction() {
        {
            putValue(NAME, "Renommer la srie");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            menu.setVisible(false);
            String newName = "";
            while (newName == null || newName.equals("")) {
                newName = (String) JOptionPane.showInputDialog(application,
                        "Entrez un nouveau nom pour la srie", null, JOptionPane.QUESTION_MESSAGE, null, null,
                        series.getName());
            }
            series.setName(newName);
        }
    });

    if (series.hasOwnAxis()) {
        menu.add(new AbstractAction() {

            {
                putValue(NAME, "Supprimer l'axe spcifique");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(application, "tes vous sr de vouloir supprimer cet axe ?",
                        "Confirmation", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                    series.setAxis(null);
                }
            }
        });
    } else {
        menu.add(new JMenuItem(new AbstractAction() {

            {
                putValue(NAME, "Crer un axe spcifique pour la srie");
            }

            @Override
            public void actionPerformed(ActionEvent e) {
                String name = JOptionPane.showInputDialog(application, "Quel titre pour cet axe ?",
                        series.getAxis().getLabel());
                if (name == null || "".equals(name))
                    return; // User has canceled
                series.setAxis(new NumberAxis(name));
            }
        }));
    }

    menu.add(new SetTypeMenu(series));

    if (series.isWater()) {
        menu.addSeparator();
        menu.add(new SumOnPeriodAction(series));
        menu.add(new CreateCumulAction(series));
    }
    if (series.isWaterCumul()) {
        menu.addSeparator();
        menu.add(new SamplingAction(series));
    }

    if (series.isPressure()) {
        menu.addSeparator();
        menu.add(new CorrelateAction(series));
        menu.add(new WaterHeightAction(series));
    }

    menu.addSeparator();

    menu.add(new AbstractAction() {
        {
            String name;
            if (series.canUndo())
                name = "Annuler " + series.getItemsName();
            else
                name = series.getLastUndoName();

            putValue(NAME, name);

            if (series.canUndo())
                setEnabled(true);
            else {
                setEnabled(false);
            }

        }

        @Override
        public void actionPerformed(ActionEvent e) {
            series.undo();
        }
    });

    menu.add(new AbstractAction() {
        {
            String name;
            if (series.canRedo()) {
                name = "Refaire " + series.getNextRedoName();
                setEnabled(true);
            } else {
                name = series.getNextRedoName();
                setEnabled(false);
            }

            putValue(NAME, name);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            series.redo();
        }
    });

    menu.add(new AbstractAction() {
        {
            putValue(NAME, I18nSupport.translate("menus.serie.resetSerie"));
            if (series.canUndo())
                setEnabled(true);
            else
                setEnabled(false);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            series.reset();
        }
    });

    menu.add(new LimitDateRangeAction(series));

    menu.add(new HourSettingAction(series));

    menu.addSeparator();

    {
        JMenuItem deleteItem = new JMenuItem("Supprimer la srie");
        deleteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(application,
                        "tes-vous sur de vouloir supprimer cette srie ?\n"
                                + "Cette action est dfinitive.",
                        "Confirmation", JOptionPane.OK_CANCEL_OPTION,
                        JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
                    series.delete();
                }
            }
        });
        menu.add(deleteItem);
    }

    menu.addSeparator();

    {
        final JMenuItem up = new JMenuItem("Remonter dans la liste"),
                down = new JMenuItem("Descendre dans la liste");
        ActionListener listener = new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource().equals(up)) {
                    series.upSeriesInList();
                } else {
                    series.downSeriesInList();
                }
            }
        };
        up.addActionListener(listener);
        down.addActionListener(listener);
        if (series.isFirst()) {
            menu.add(down);
        } else if (series.isLast()) {
            menu.add(up);
        } else {
            menu.add(up);
            menu.add(down);
        }
    }

    menu.addSeparator();

    {
        menu.add(new SeriesInfoAction(series));
    }

    {
        JMenuItem colorItem = new JMenuItem("Couleur de la srie");
        colorItem.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                series.setColor(JColorChooser.showDialog(application,
                        I18nSupport.translate("actions.selectColorForSeries"), series.getColor()));
            }
        });
        menu.add(colorItem);
    }

    {
        JMenu plotRenderer = new JMenu("Affichage de la srie");
        final ButtonGroup modes = new ButtonGroup();
        java.util.List<DrawStyle> availableStyles;
        if (series.isMinMax()) {
            availableStyles = DrawStyles.getDrawableStylesForHighLow();
        } else {
            availableStyles = DrawStyles.getDrawableStyles();
        }
        for (final DrawStyle s : availableStyles) {
            final JRadioButtonMenuItem item = new JRadioButtonMenuItem(DrawStyles.getHumanCheckboxText(s));
            item.addChangeListener(new ChangeListener() {
                @Override
                public void stateChanged(ChangeEvent e) {
                    if (item.isSelected())
                        series.setStyle(s);
                }
            });
            modes.add(item);
            if (s.equals(series.getStyle())) {
                modes.setSelected(item.getModel(), true);
            }
            plotRenderer.add(item);
        }
        menu.add(plotRenderer);
    }
    menu.addSeparator();

    menu.add(new AbstractAction() {
        {
            putValue(Action.NAME, "Fermer le fichier");
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (JOptionPane.showConfirmDialog(application,
                    "tes-vous sur de vouloir fermer toutes les sries du fichier ?", "Confirmation",
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.OK_OPTION) {
                final File f = series.getOrigin();
                for (final Series s : Series.getInstances().toArray(new Series[Series.getInstances().size()])) {
                    if (s.getOrigin().equals(f))
                        s.delete();
                }
            }
        }
    });

    return menu;
}