Java Tutorial/Swing Event/ItemListener
Содержание
ComboBox with ItemListener
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ComboBoxWithItemChangedListener extends JFrame {
public static void main(String[] args) {
ComboBoxWithItemChangedListener that = new ComboBoxWithItemChangedListener();
that.setVisible(true);
}
public ComboBoxWithItemChangedListener() {
setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new ComboPanel(), BorderLayout.SOUTH);
}
}
class ComboPanel extends JPanel {
final static String[] treasure = { "Gold", "Silver", "Diamonds", "Rubies", "Emeralds",
"Sapphires", "Chocolate" };
public ComboPanel() {
final JComboBox combo = new JComboBox(treasure);
combo.setMaximumRowCount(5);
combo.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
System.out.println("Combo: " + combo.getSelectedItem());
}
});
combo.setSelectedIndex(4);
add(combo);
}
}
Firing Item Events
import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
public class Main {
public static void main(String[] argv) throws Exception {
MyComponent component = new MyComponent();
JFrame f = new JFrame();
f.add(component);
f.setSize(300, 300);
f.setVisible(true);
}
}
class MyComponent extends JTextField implements ItemSelectable {
protected EventListenerList listenerList = new EventListenerList();
public Object[] getSelectedObjects() {
return new String[] { "a", "b", "c" };
}
public void addItemListener(ItemListener l) {
listenerList.add(ItemListener.class, l);
}
public void removeItemListener(ItemListener l) {
listenerList.remove(ItemListener.class, l);
}
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);
}
}
}
}
How to Write an Item Listener
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.AbstractButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class JCheckBoxItemListener {
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);
}
}
};
aCheckBox4.addItemListener(itemListener);
frame.add(aCheckBox4);
frame.setSize(300, 200);
frame.setVisible(true);
}
}
ItemListener for JRadioButton
import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Container;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.WindowConstants;
public class Main extends JFrame {
private JRadioButton small, medium, large;
private JButton button;
public Main(String title) {
super(title);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = this.getContentPane();
ButtonGroup group = new ButtonGroup();
small = new JRadioButton("small");
medium = new JRadioButton("medium");
large = new JRadioButton("large");
group.add(small);
group.add(medium);
group.add(large);
button = new JButton("Click here.");
button.setBounds(100, 50, 100, 50);
JPanel center = new JPanel();
center.setLayout(null);
center.add(button);
contentPane.add(center, BorderLayout.CENTER);
JPanel north = new JPanel();
north.add(small);
north.add(medium);
north.add(large);
contentPane.add(north, BorderLayout.NORTH);
ChangeSize listener = new ChangeSize(button);
small.addItemListener(listener);
medium.addItemListener(listener);
large.addItemListener(listener);
}
public static void main(String[] args) {
JFrame f = new Main("JRadioButtonDemo");
f.setSize(300, 200);
f.setVisible(true);
}
}
class ChangeSize implements ItemListener {
private Component component;
public ChangeSize(Component c) {
component = c;
}
public void itemStateChanged(ItemEvent e) {
String size = (String) e.getItem();
if (size.equals("small")) {
component.setSize(75, 20);
} else if (size.equals("medium")) {
component.setSize(100, 50);
} else if (size.equals("large")) {
component.setSize(150, 75);
}
}
}
Toggle button with ItemListener
import java.awt.BorderLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JToggleButton;
public class ToggleButtonCheckBoxRadioButton extends JFrame {
public static void main(String[] args) {
ToggleButtonCheckBoxRadioButton that = new ToggleButtonCheckBoxRadioButton();
that.setVisible(true);
}
public ToggleButtonCheckBoxRadioButton() {
setSize(450, 350);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().add(new TogglePanel(), BorderLayout.SOUTH);
}
}
class TogglePanel extends JPanel {
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());
}
};
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);
}
}
}