Example usage for javax.swing JCheckBoxMenuItem JCheckBoxMenuItem

List of usage examples for javax.swing JCheckBoxMenuItem JCheckBoxMenuItem

Introduction

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

Prototype

public JCheckBoxMenuItem(Action a) 

Source Link

Document

Creates a menu item whose properties are taken from the Action supplied.

Usage

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);//  ww w. ja va 2 s . com

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ItemListener iListener = new ItemListener() {
        public void itemStateChanged(ItemEvent event) {
            Icon girlIcon = new ImageIcon("g.jpg");
            Icon boyIcon = new ImageIcon("b.jpg");
            AbstractButton aButton = (AbstractButton) event.getSource();
            int state = event.getStateChange();
            String newLabel;
            Icon newIcon;
            if (state == ItemEvent.SELECTED) {
                newLabel = "Girl";
                newIcon = girlIcon;
            } else {
                newLabel = "Boy";
                newIcon = boyIcon;
            }
            aButton.setText(newLabel);
            aButton.setIcon(newIcon);
        }
    };

    caseMenuItem.addItemListener(iListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    menuBar.add(fileMenu);/*  www  . j  ava  2  s.  com*/

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    newMenuItem.setAccelerator(KeyStroke.getKeyStroke("N"));
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            String newLabel;
            if (selected) {
                newLabel = "A";
            } else {
                newLabel = "B";
            }
            aButton.setText(newLabel);
        }
    };

    caseMenuItem.addActionListener(aListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:JCheckBoxMenuItemActionListener.java

public static void main(final String args[]) {
    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);/*from   w ww.  ja va  2 s .com*/

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem("Case Sensitive");
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    ItemListener iListener = new ItemListener() {
        public void itemStateChanged(ItemEvent event) {
            Icon girlIcon = new ImageIcon("girl-r.jpg");
            Icon boyIcon = new ImageIcon("boy-r.jpg");
            AbstractButton aButton = (AbstractButton) event.getSource();
            int state = event.getStateChange();
            String newLabel;
            Icon newIcon;
            if (state == ItemEvent.SELECTED) {
                newLabel = "Girl";
                newIcon = girlIcon;
            } else {
                newLabel = "Boy";
                newIcon = boyIcon;
            }
            aButton.setText(newLabel);
            aButton.setIcon(newIcon);
        }
    };

    caseMenuItem.addItemListener(iListener);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    Action action = new AbstractAction("CheckBox Label") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("called");
        }/*from   ww  w.j ava 2s. c  o m*/
    };

    JFrame frame = new JFrame("MenuSample Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();

    // File Menu, F - Mnemonic
    JMenu fileMenu = new JMenu("File");
    fileMenu.setMnemonic(KeyEvent.VK_F);
    menuBar.add(fileMenu);

    // File->New, N - Mnemonic
    JMenuItem newMenuItem = new JMenuItem("New", KeyEvent.VK_N);
    fileMenu.add(newMenuItem);

    JCheckBoxMenuItem caseMenuItem = new JCheckBoxMenuItem(action);
    caseMenuItem.setMnemonic(KeyEvent.VK_C);
    fileMenu.add(caseMenuItem);

    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);//from   ww  w.j  a v  a2s .c  o  m
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);

    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:AButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);//from w  ww .  jav  a 2  s.c o m
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:AButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);/*w w  w  .  j  a  v  a  2  s.c o  m*/
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:FindingAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);/*  w w w  .j  a  va  2 s  .co m*/
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);
    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);
    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);
    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);
    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);
    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

From source file:SettingSelectedAButtonGroup.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Button Group");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new GridLayout(0, 1));
    Border border = BorderFactory.createTitledBorder("Examples");
    panel.setBorder(border);//from ww w  .j a  v a2s  .  c o  m
    ButtonGroup group = new ButtonGroup();
    AbstractButton abstract1 = new JToggleButton("Toggle Button");
    panel.add(abstract1);
    group.add(abstract1);

    AbstractButton abstract2 = new JRadioButton("Radio Button");
    panel.add(abstract2);
    group.add(abstract2);

    AbstractButton abstract3 = new JCheckBox("Check Box");
    panel.add(abstract3);
    group.add(abstract3);

    AbstractButton abstract4 = new JRadioButtonMenuItem("Radio Button Menu Item");
    panel.add(abstract4);
    group.add(abstract4);

    AbstractButton abstract5 = new JCheckBoxMenuItem("Check Box Menu Item");
    panel.add(abstract5);
    group.add(abstract5);
    frame.add(panel, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
    group.setSelected(abstract1.getModel(), true);

    Enumeration elements = group.getElements();
    while (elements.hasMoreElements()) {
        AbstractButton button = (AbstractButton) elements.nextElement();
        if (button.isSelected()) {
            System.out.println("The winner is: " + button.getText());
        }
    }
}

From source file:ToggleSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("JToggleButtonMenuItem Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu("File");
    file.setMnemonic('f');
    JMenuItem newItem = new JMenuItem("New", 'N');
    file.add(newItem);//  ww  w .j  a  v  a2 s  . co  m
    JMenuItem openItem = new JMenuItem("Open", 'O');
    file.add(openItem);
    JMenuItem closeItem = new JMenuItem("Close", 'C');
    file.add(closeItem);
    file.addSeparator();
    JMenuItem saveItem = new JMenuItem("Save", 'S');
    file.add(saveItem);
    file.addSeparator();
    JMenuItem exitItem = new JMenuItem("Exit", 'X');
    file.add(exitItem);
    bar.add(file);
    JMenu edit = new JMenu("Edit");
    JMenuItem cutItem = new JMenuItem("Cut", 'T');
    cutItem.setAccelerator(KeyStroke.getKeyStroke('X', Event.CTRL_MASK));
    edit.add(cutItem);
    JMenuItem copyItem = new JMenuItem("Copy", 'C');
    copyItem.setAccelerator(KeyStroke.getKeyStroke('C', Event.CTRL_MASK));
    edit.add(copyItem);
    JMenuItem pasteItem = new JMenuItem("Paste", 'P');
    pasteItem.setAccelerator(KeyStroke.getKeyStroke('V', Event.CTRL_MASK));
    pasteItem.setEnabled(false);
    edit.add(pasteItem);
    edit.addSeparator();
    JMenuItem findItem = new JMenuItem("Find", 'F');
    findItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F3, 0));
    edit.add(findItem);
    edit.setMnemonic('e');
    Icon atIcon = new ImageIcon("at.gif");
    JMenu findOptions = new JMenu("Options");
    findOptions.setIcon(atIcon);
    findOptions.setMnemonic('O');
    ButtonGroup directionGroup = new ButtonGroup();
    JRadioButtonMenuItem forward = new JRadioButtonMenuItem("Forward", true);
    findOptions.add(forward);
    directionGroup.add(forward);
    JRadioButtonMenuItem backward = new JRadioButtonMenuItem("Backward");
    findOptions.add(backward);
    directionGroup.add(backward);
    findOptions.addSeparator();
    JCheckBoxMenuItem caseItem = new JCheckBoxMenuItem("Case Insensitive");
    findOptions.add(caseItem);
    edit.add(findOptions);
    JToggleButtonMenuItem toggleItem = new JToggleButtonMenuItem("Ballon Help");
    toggleItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Selected");
        }
    });
    edit.add(toggleItem);
    bar.add(edit);
    frame.setJMenuBar(bar);
    frame.setSize(350, 250);
    frame.setVisible(true);
}