Java ItemSelectable .getSelectedObjects ()
Syntax
ItemSelectable.getSelectedObjects() has the following syntax.
Object [] getSelectedObjects()
Example
In the following code shows how to use ItemSelectable.getSelectedObjects() method.
import java.awt.BorderLayout;
import java.awt.ItemSelectable;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
// w w w .j a va 2s .c om
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class Main {
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]);
}
}
Home »
Java Tutorial »
java.awt »
Java Tutorial »
java.awt »