Example usage for java.awt.event ItemEvent getStateChange

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

Introduction

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

Prototype

public int getStateChange() 

Source Link

Document

Returns the type of state change (selected or deselected).

Usage

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());
            }// ww w .  j a v  a 2  s.  co m
        }
    });
    JFrame frame = new JFrame();
    frame.getContentPane().add(box);
    frame.pack();
    frame.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");
            }// ww w  . j a  v  a2 s . co m
        }
    };
    // 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(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));
        }/*from w  ww  .j  a  v  a 2  s  .c o m*/
    };
    comboBox.addItemListener(itemListener);
    frame.setSize(400, 200);
    frame.setVisible(true);

}

From source file:JToggleButtonEventListenerSequence.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");
    // Define ActionListener
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }/*from ww w .j  a v a  2  s.  com*/
    };
    // Define ChangeListener
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    // Define ItemListener
    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");
            }
        }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    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();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JToggleButton toggleButton = new JToggleButton("Selected");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//from w w  w  . j av a 2 s  .c om
    };
    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };
    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");
            }
        }
    };
    // Attach Listeners
    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(toggleButton, BorderLayout.NORTH);

    JToggleButton toggleButton2 = new JToggleButton("Focused");
    contentPane.add(toggleButton2, BorderLayout.CENTER);

    JToggleButton toggleButton3 = new JToggleButton("Not Selected");
    contentPane.add(toggleButton3, BorderLayout.SOUTH);

    frame.setSize(300, 125);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JToggleButton toggleButton = new JToggleButton("Selected");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton abstractButton = (AbstractButton) actionEvent.getSource();
            boolean selected = abstractButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }/*from w  ww.j a v a2 s . c  o m*/
    };

    ChangeListener changeListener = new ChangeListener() {
        public void stateChanged(ChangeEvent changeEvent) {
            AbstractButton abstractButton = (AbstractButton) changeEvent.getSource();
            ButtonModel buttonModel = abstractButton.getModel();
            boolean armed = buttonModel.isArmed();
            boolean pressed = buttonModel.isPressed();
            boolean selected = buttonModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

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

    toggleButton.addActionListener(actionListener);
    toggleButton.addChangeListener(changeListener);
    toggleButton.addItemListener(itemListener);
    frame.add(toggleButton, BorderLayout.NORTH);

    JToggleButton toggleButton2 = new JToggleButton("Focused");
    frame.add(toggleButton2, BorderLayout.CENTER);

    JToggleButton toggleButton3 = new JToggleButton("Not Selected");
    frame.add(toggleButton3, BorderLayout.SOUTH);

    frame.setSize(300, 125);
    frame.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 w  w. j  a  v  a 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);
}

From source file:SelectingComboSample.java

public static void main(String args[]) {
    String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "I" };
    JFrame frame = new JFrame("Selecting JComboBox");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

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

    final JTextArea textArea = new JTextArea();
    textArea.setEditable(false);//from  w ww  .  j ava2 s  . c o  m
    JScrollPane sp = new JScrollPane(textArea);
    contentPane.add(sp, BorderLayout.CENTER);

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            int state = itemEvent.getStateChange();
            String stateString = ((state == ItemEvent.SELECTED) ? "Selected" : "Deselected");
            pw.print("Item: " + itemEvent.getItem());
            pw.print(", State: " + stateString);
            ItemSelectable is = itemEvent.getItemSelectable();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addItemListener(itemListener);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.print("Command: " + actionEvent.getActionCommand());
            ItemSelectable is = (ItemSelectable) actionEvent.getSource();
            pw.print(", Selected: " + selectedString(is));
            pw.println();
            textArea.append(sw.toString());
        }
    };
    comboBox.addActionListener(actionListener);

    frame.setSize(400, 200);
    frame.setVisible(true);
}

From source file:RadioButtonSample.java

public static void main(String args[]) {

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            AbstractButton aButton = (AbstractButton) actionEvent.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println(actionEvent.getActionCommand() + " - selected? " + selected);
        }/*from   ww  w.  j ava  2s. c o  m*/
    };

    ItemListener itemListener = new ItemListener() {
        public void itemStateChanged(ItemEvent itemEvent) {
            AbstractButton aButton = (AbstractButton) itemEvent.getSource();
            int state = itemEvent.getStateChange();
            String selected = ((state == ItemEvent.SELECTED) ? "selected" : "not selected");
            System.out.println(aButton.getText() + " - selected? " + selected);
        }
    };

    JFrame frame = new JFrame("Radio Menu Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenuBar menuBar = new JMenuBar();
    JMenu menu = new JMenu("Menu");
    ButtonGroup buttonGroup = new ButtonGroup();
    menu.setMnemonic(KeyEvent.VK_M);

    JRadioButtonMenuItem emptyMenuItem = new JRadioButtonMenuItem();
    emptyMenuItem.setActionCommand("Empty");
    emptyMenuItem.addActionListener(actionListener);
    buttonGroup.add(emptyMenuItem);
    menu.add(emptyMenuItem);

    JRadioButtonMenuItem oneMenuItem = new JRadioButtonMenuItem("Partridge");
    oneMenuItem.addActionListener(actionListener);
    buttonGroup.add(oneMenuItem);
    menu.add(oneMenuItem);

    JRadioButtonMenuItem twoMenuItem = new JRadioButtonMenuItem("Turtle Doves", true);
    twoMenuItem.addActionListener(actionListener);
    buttonGroup.add(twoMenuItem);
    menu.add(twoMenuItem);

    JRadioButtonMenuItem threeMenuItem = new JRadioButtonMenuItem("French Hens", threeIcon);
    threeMenuItem.addItemListener(itemListener);
    buttonGroup.add(threeMenuItem);
    menu.add(threeMenuItem);

    JRadioButtonMenuItem fourMenuItem = new JRadioButtonMenuItem("Calling Birds", fourIcon, true);
    fourMenuItem.addActionListener(actionListener);
    buttonGroup.add(fourMenuItem);
    menu.add(fourMenuItem);

    JRadioButtonMenuItem fiveMenuItem = new JRadioButtonMenuItem(fiveIcon);
    fiveMenuItem.addActionListener(actionListener);
    fiveMenuItem.setActionCommand("Rings");
    buttonGroup.add(fiveMenuItem);
    menu.add(fiveMenuItem);

    JRadioButtonMenuItem sixMenuItem = new JRadioButtonMenuItem(sixIcon, true);
    sixMenuItem.addActionListener(actionListener);
    sixMenuItem.setActionCommand("Geese");
    buttonGroup.add(sixMenuItem);
    menu.add(sixMenuItem);

    menuBar.add(menu);
    frame.setJMenuBar(menuBar);
    frame.setSize(350, 250);
    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"));
        }//from   ww w . j  av a2  s  .c  om

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