Example usage for java.awt.event ItemEvent getSource

List of usage examples for java.awt.event ItemEvent getSource

Introduction

In this page you can find the example usage for java.awt.event ItemEvent getSource.

Prototype

public Object getSource() 

Source Link

Document

The object on which the Event initially occurred.

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  .j a  v a  2s  .  co m*/

    // 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: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 ww  w.  jav a2 s . c  o  m

    // 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:GroupActionRadio.java

public static void main(String args[]) {

    String title = (args.length == 0 ? "Grouping Example" : args[0]);
    JFrame frame = new JFrame(title);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Slice Parts
    ActionListener sliceActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            System.out.println("Selected: " + aButton.getText());
        }/* www . j  a v a  2  s . co m*/
    };
    Container sliceContainer = RadioButtonUtils.createRadioButtonGrouping(sliceOptions, "Slice Count",
            sliceActionListener);

    // Crust Parts
    ActionListener crustActionListener = new ActionListener() {
        String lastSelected;

        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            String label = aButton.getText();
            String msgStart;
            if (label.equals(lastSelected)) {
                msgStart = "Reselected: ";
            } else {
                msgStart = "Selected: ";
            }
            lastSelected = label;
            System.out.println(msgStart + label);
        }
    };
    ItemListener itemListener = new ItemListener() {
        String lastSelected;

        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String label = aButton.getText();
            String msgStart;
            if (state == ItemEvent.SELECTED) {
                if (label.equals(lastSelected)) {
                    msgStart = "Reselected -> ";
                } else {
                    msgStart = "Selected -> ";
                }
                lastSelected = label;
            } else {
                msgStart = "Deselected -> ";
            }
            System.out.println(msgStart + label);
        }
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changEvent) {
            AbstractButton aButton = (AbstractButton) changEvent.getSource();
            ButtonModel aModel = aButton.getModel();
            boolean armed = aModel.isArmed();
            boolean pressed = aModel.isPressed();
            boolean selected = aModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    final Container crustContainer = RadioButtonUtils.createRadioButtonGrouping(crustOptions, "Crust Type",
            crustActionListener, itemListener, changeListener);

    // Button Parts
    ActionListener buttonActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Enumeration selected = RadioButtonUtils.getSelectedElements(crustContainer);
            while (selected.hasMoreElements()) {
                System.out.println("Selected -> " + selected.nextElement());
            }
        }
    };
    JButton button = new JButton("Order Pizza");
    button.addActionListener(buttonActionListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(sliceContainer, BorderLayout.WEST);
    contentPane.add(crustContainer, BorderLayout.EAST);
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public void itemStateChanged(ItemEvent evt) {
    JComboBox cb = (JComboBox) evt.getSource();

    Object item = evt.getItem();//from ww w.j  ava2 s.  c  o  m

    if (evt.getStateChange() == ItemEvent.SELECTED) {
        // Item was just selected
    } else if (evt.getStateChange() == ItemEvent.DESELECTED) {
        // Item is no longer selected
    }
}

From source file:ToggleButtonCheckBoxRadioButton.java

public TogglePanel() {
    JToggleButton tog = new JToggleButton("Toggle");
    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            AbstractButton src = (AbstractButton) (e.getSource());
            System.out.println("Toggle: " + src.getText());
        }//from   w ww. ja  v  a  2  s . c  o m
    };
    tog.addItemListener(listener);
    add(tog);

    JCheckBox cbox = new JCheckBox("Checkbox");
    cbox.addItemListener(listener);
    add(cbox);
    ButtonGroup btngroup = new ButtonGroup();
    for (int i = 1; i <= 3; i++) {
        JRadioButton radio = new JRadioButton("Radio " + i);
        btngroup.add(radio);
        radio.addItemListener(listener);
        add(radio);
    }
}

From source file:ComboBox.java

public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() == ItemEvent.SELECTED) {
        JComboBox combo = (JComboBox) e.getSource();
        int index = combo.getSelectedIndex();
        display.setIcon(new ImageIcon(ClassLoader.getSystemResource(images[index])));
    }// w  w w.  j  av  a 2 s .c o  m
}

From source file:org.jcurl.demo.tactics.CurlerSwingBean.java

public void itemStateChanged(final ItemEvent e) {
    log.warn("Unconsumed event from " + e.getSource());
}

From source file:MenuDemo.java

public void itemStateChanged(ItemEvent e) {
    JMenuItem source = (JMenuItem) (e.getSource());
    String s = "Item event detected." + newline + "    Event source: " + source.getText() + " (an instance of "
            + getClassName(source) + ")" + newline + "    New state: "
            + ((e.getStateChange() == ItemEvent.SELECTED) ? "selected" : "unselected");
    output.append(s + newline);//from  www  .  j  a v a 2  s .  c  o  m
    output.setCaretPosition(output.getDocument().getLength());
}

From source file:Composite.java

public void itemStateChanged(ItemEvent e) {
    if (e.getStateChange() != ItemEvent.SELECTED) {
        return;/*from ww w. j a v  a2s .  c om*/
    }

    Object choice = e.getSource();
    if (choice == alphas) {
        alpha = (String) alphas.getSelectedItem();
    } else {
        rule = rules.getSelectedIndex();
    }
    comp.changeRule(alpha, rule);
}

From source file:org.pentaho.ui.xul.swing.tags.SwingCheckbox.java

public SwingCheckbox(Element self, XulComponent parent, XulDomContainer domContainer, String tagName) {
    super("checkbox");
    checkBox = new JCheckBox();

    checkBox.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            SwingCheckbox.this.changeSupport.firePropertyChange("checked", null,
                    ((JCheckBox) e.getSource()).isSelected());
        }/*from   w w  w .ja va 2  s . c o  m*/
    });

    setManagedObject(checkBox);
}