List of usage examples for java.awt.event ItemEvent SELECTED
int SELECTED
To view the source code for java.awt.event ItemEvent SELECTED.
Click Source Link
From source file:SelectingCheckBox.java
public static void main(String args[]) { JFrame frame = new JFrame("Selecting CheckBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JCheckBox checkBox = new JCheckBox(DESELECTED_LABEL); // Define ActionListener ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { AbstractButton abstractButton = (AbstractButton) actionEvent.getSource(); boolean selected = abstractButton.getModel().isSelected(); String newLabel = (selected ? SELECTED_LABEL : DESELECTED_LABEL); abstractButton.setText(newLabel); }/* w w w . ja va 2s . 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) { 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); } } }; checkBox.addActionListener(actionListener); checkBox.addChangeListener(changeListener); checkBox.addItemListener(itemListener); checkBox.setMnemonic(KeyEvent.VK_S); Container contentPane = frame.getContentPane(); contentPane.add(checkBox, BorderLayout.NORTH); frame.setSize(300, 100); 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()); }/*from www .j a v a2s. c o 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 ie) { JCheckBox cb = (JCheckBox) ie.getItem(); int state = ie.getStateChange(); if (state == ItemEvent.SELECTED) { System.out.println(cb.getText() + " selected"); } else {/* ww w . j ava 2 s. c o m*/ System.out.println(cb.getText() + " cleared"); } }
From source file:Main.java
public Main() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); String[] sList = new String[] { "Spring", "Summer", "Fall", "Winter" }; JComboBox<String> seasons = new JComboBox<>(sList); seasons.addItemListener((ItemEvent e) -> { Object item = e.getItem(); if (e.getStateChange() == ItemEvent.SELECTED) { // Item has been selected System.out.println(item + " has been selected"); } else if (e.getStateChange() == ItemEvent.DESELECTED) { // Item has been deselected System.out.println(item + " has been deselected"); }//from w w w . j a va 2s . c o m }); Container contentPane = this.getContentPane(); contentPane.add(seasons, BorderLayout.CENTER); }
From source file:Main.java
public void itemStateChanged(ItemEvent evt) { JComboBox cb = (JComboBox) evt.getSource(); Object item = evt.getItem();//from w w w . j a v a2s . 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:Main.java
public void itemStateChanged(ItemEvent ie) { JCheckBox cb = (JCheckBox) ie.getItem(); int state = ie.getStateChange(); if (state == ItemEvent.SELECTED) System.out.println(cb.getText() + " selected"); else//from ww w . ja v a 2 s .c o m System.out.println(cb.getText() + " cleared"); }
From source file:Main.java
void fireItemEvent(Object item, boolean sel) { ItemEvent evt = new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, item, sel ? ItemEvent.SELECTED : ItemEvent.DESELECTED); Object[] listeners = listenerList.getListenerList(); for (int i = 0; i < listeners.length - 2; i += 2) { if (listeners[i] == ItemListener.class) { ((ItemListener) listeners[i + 1]).itemStateChanged(evt); }//from w w w . java2 s . co m } }
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]))); }/*from www . java2s . c o m*/ }
From source file:ItemEventComponent.java
public void processEvent(AWTEvent e) { if (e.getID() == MouseEvent.MOUSE_PRESSED) { if (itemListener != null) { selected = !selected;/*from w w w . ja va 2s.com*/ i++; itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED, getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED))); } } }
From source file:SystemColorChooserPanel.java
public void itemStateChanged(ItemEvent itemEvent) { int state = itemEvent.getStateChange(); if (state == ItemEvent.SELECTED) { int position = findColorLabel(itemEvent.getItem()); if ((position != NOT_FOUND) && (position != labels.length - 1)) { ColorSelectionModel selectionModel = getColorSelectionModel(); selectionModel.setSelectedColor(colors[position]); }//from w w w .j a v a 2 s .c om } }