Java Reference
In-Depth Information
Collection<Object> col = System.getProperties().values();
ArrayList<Object> arrayList = new ArrayList<Object>(col);
ArrayListComboBoxModel model = new ArrayListComboBoxModel(arrayList);
JComboBox comboBox = new JComboBox (model);
frame.add(comboBox, BorderLayout.NORTH);
frame.setSize(300, 225);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Figure 13-3 shows the model in action using the current system properties as the source
for the data model elements.
Figure 13-3.
Using an ArrayListComboBoxModel
JList Class
The
JList
component is the basic Swing component for selecting one or more items from a set
of choices. You present the list of choices to the user, and the user can pick one or several,
depending on the selection mode of the component.
Three key elements and their implementations define the
JList
structure:
A data model for holding the
JList
data, as defined by the
ListModel
interface
•
A cell renderer for drawing the elements of the
JList
, as described by the
ListCellRenderer
interface
•
•
A selection model for selecting elements of the
JList
, as described by the
ListSelectionModel
interface
Creating JList Components
The
JList
component has four constructors, which allow you to create a
JList
instance based
on your initial data structure: