

Another possible value is JList.VERTICAL_WRAP, which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column. The value JList.HORIZONTAL_WRAP specifies that the list should display its items from left to right before wrapping to a new row.

The call to setLayoutOrientation lets the list display its data in multiple columns. The call to setSelectionMode specifies how many items the user can select, and whether they must be contiguous the next section tells you more about selection modes. See Adding Items to and Removing Items from a List for an example. You can set a list's model when you create the list or by calling the setModel method. To create a list whose items can be changed individually, set the list's model to an instance of a mutable list model class, such as an instance ofĭefaultListModel. The default list model is immutable you cannot add, remove, or replace items in the list. If you initialize a list with an array or vector, the constructor implicitly creates a default list model. Other JList constructors let you initialize a list from a Vector or from an object that adheres to the In our example, the strings happen to be boys' names. The array is filled with strings that were passed in from another object. The code passes an array to the list's constructor. JScrollPane listScroller = new JScrollPane(list) tLayoutOrientation(JList.HORIZONTAL_WRAP) tSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION) List = new JList(data) //data has type Object ListDialog.java that creates and sets up its list: For this approach, you must subclass AbstractListModel and implement the getSize and getElementAt methods inherited from the ListModel interface.

