The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JList  [12 examples]

e774. Creating a JList Component

By default, a list allows more than one item to be selected. Also, the selected items need not be contiguous. To change this default, see e781 Setting the Selection Mode of a JList Component.

A list selection event is fired when the set of selected items is changed (see e784 Listening for Changes to the Selection in a JList Component).

    // Create a list with some items
    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);
The items can be arbitrary objects. The toString() method of the objects is displayed in the list component.
    // Create a list with two items - "123" and "Sun May 19 21:15:38 PDT 2002"
    Object[] items2 = {new Integer(123), new java.util.Date()};
    list = new JList(items2);
By default, a list does not automatically display a scrollbar when there are more items than can be displayed. The list must be wrapped in a scroll pane:
    JScrollPane scrollingList = new JScrollPane(list);

 Related Examples
e775. Setting the Dimensions of an Item in a JList Component
e776. Setting a Tool Tip for an Item in a JList Component
e777. Getting the Items in a JList Component
e778. Adding and Removing an Item in a JList Component
e779. Getting the Selected Items in a JList Component
e780. Setting the Selected Items in a JList Component
e781. Setting the Selection Mode of a JList Component
e782. Arranging Items in a JList Component
e783. Detecting Double and Triple Clicks on an Item in a JList Component
e784. Listening for Changes to the Selection in a JList Component
e785. Listening for Changes to the Items in a JList Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.