Java by API/java.awt.event/ItemEvent
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
ItemEvent: getItem()
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
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
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}
ItemEvent: getItemSelectable()
import java.awt.BorderLayout;
import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class MainClass {
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));
}
};
comboBox.addItemListener(itemListener);
frame.setSize(400, 200);
frame.setVisible(true);
}
static private String selectedString(ItemSelectable is) {
Object selected[] = is.getSelectedObjects();
return ((selected.length == 0) ? "null" : (String)selected[0]);
}
}
ItemEvent: getStateChange()
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
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
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}
ItemEvent.SELECTED
import java.awt.Color;
import java.awt.ruponent;
import java.awt.Graphics;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel implements ItemListener {
public MainClass() {
// Add check boxes to the content pane.
Icon normal = new MyIcon(Color.red);
Icon rollover = new MyIcon(Color.YELLOW);
Icon selected = new MyIcon(Color.BLUE);
JCheckBox cb = new JCheckBox("www.jexp.ru", normal);
cb.setRolloverIcon(rollover);
cb.setSelectedIcon(selected);
cb.addItemListener(this);
add(cb);
}
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
System.out.println(cb.getText() + " cleared");
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.getContentPane().add(new MainClass());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setVisible(true);
}
}
class MyIcon implements Icon {
Color cl;
public MyIcon(Color c) {
cl = c;
}
public int getIconWidth() {
return 32;
}
public int getIconHeight() {
return 32;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(cl);
g.drawString("jexp.ru", 0, 20);
}
}