Example usage for java.awt.event ItemListener ItemListener

List of usage examples for java.awt.event ItemListener ItemListener

Introduction

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

Prototype

ItemListener

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }//from ww  w  .  j ava 2s.com
    });
    ItemListener[] lis = jb.getItemListeners();
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JComboBox<String> box = new JComboBox<>();
    box.addItem("One");
    box.addItem("Two");
    box.addItem("Three");

    box.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                System.out.println(e.getItem());
            }//from  ww w .j a  va  2  s .c o m
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] args) {
    JButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }/*  ww  w.  j  a  va 2s .com*/
    });

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }//  www.j a v  a2  s .c  om
    });

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Selecting Toggle");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Toggle Button");

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                System.out.println("Selected");
            } else {
                System.out.println("Deselected");
            }/*from ww w.  j  ava2 s  .c om*/
        }
    };
    // Attach Listeners
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);
    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:Main.java

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

    JCheckBox aCheckBox4 = new JCheckBox("Stuffed Crust");

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton abstractButton = (AbstractButton) itemEvent.getSource();
            Color foreground = abstractButton.getForeground();
            Color background = abstractButton.getBackground();
            int state = itemEvent.getStateChange();
            if (state == ItemEvent.SELECTED) {
                abstractButton.setForeground(background);
                abstractButton.setBackground(foreground);
            }/*from  w  ww. j  av a2  s . co m*/
        }
    };
    aCheckBox4.addItemListener(itemListener);
    frame.add(aCheckBox4);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(final String args[]) {
    final String labels[] = { "A", "B", "C", "D", "E" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JComboBox comboBox = new JComboBox(labels);
    frame.add(comboBox, BorderLayout.SOUTH);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            System.out.println("Item: " + itemEvent.getItem());
            System.out.println(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            System.out.println(", Selected: " + selectedString(is));
        }// ww  w. j av a  2 s.  c om
    };
    comboBox.addItemListener(itemListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:ItemTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();

    ItemListener listener = new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            System.out.println("Source: " + name(e.getSource()));
            System.out.println("Item: " + name(e.getItem()));
            int state = e.getStateChange();
            System.out.println("State: " + ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected"));
        }// ww w . jav a 2  s  .com

        private String name(Object o) {
            if (o instanceof JComponent) {
                JComponent comp = (JComponent) o;
                return comp.getName();
            } else {
                return o.toString();
            }
        }
    };

    JPanel panel = new JPanel(new GridLayout(0, 1));
    ButtonGroup group = new ButtonGroup();
    JRadioButton option = new JRadioButton("French Fries", true);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Onion Rings", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    option = new JRadioButton("Ice Cream", false);
    option.setName(option.getText());
    option.addItemListener(listener);
    group.add(option);
    panel.add(option);
    contentPane.add(panel, BorderLayout.NORTH);

    String flavors[] = { "Item 1", "Item 2", "Item 3" };
    JComboBox jc = new JComboBox(flavors);
    jc.setName("Combo");
    jc.addItemListener(listener);
    jc.setMaximumRowCount(4);
    contentPane.add(jc, BorderLayout.SOUTH);

    frame.pack();
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    String items[] = { "Item1", "item1" };
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JComboBox<String> combo = new JComboBox<>(items);
    combo.setEditable(true);//from   w w w.j a va2s  . co m

    JTextField txt = new JTextField(10);
    panel.add(combo);
    panel.add(txt);
    f.add(panel);
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ie) {
            String str = (String) combo.getSelectedItem();
            txt.setText(str);
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 100);
    f.setVisible(true);
}

From source file:JRadioButtonItemListener.java

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

    JPanel panel = new JPanel(new GridLayout(0, 1));

    ButtonGroup group = new ButtonGroup();
    JRadioButton aRadioButton = new JRadioButton("A");
    JRadioButton bRadioButton = new JRadioButton("B");

    ItemListener itemListener = new ItemListener() {
        String lastSelected;/*from w  ww  .  ja  va  2  s  .  c  o  m*/

        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);
        }
    };

    panel.add(aRadioButton);
    group.add(aRadioButton);
    panel.add(bRadioButton);
    group.add(bRadioButton);

    aRadioButton.addItemListener(itemListener);
    bRadioButton.addItemListener(itemListener);

    frame.add(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
}