JList
presents the user with a group of items, displayed in one or more columns, to choose from. Lists can have many items, so they are often put in scroll panes. In addition to lists, the following Swing components present multiple selectable items to the user: combo boxes, menus, tables, and groups of check boxes or radio buttons. To display hierarchical data, use a tree.
The following figures shows two applications that use lists. This section uses these examples as a ba sis for the discussions that follow.
Try this:
- Click the Launch button to run ListDemo using Java™ Web Start (download JDK 6). Alternatively, to compile and run the example yourself, consult the example index.
- Click the Launch button to run ListDialogRunner. Alternatively, to compile and run the example yourself, consult the example index.
- To bring up the ListDialog, click the Pick a new name... button in the window titled Name That Baby.
The resulting dialog is a ListDialog instance that has been customized to have the title Name Chooser. - In ListDemo, try adding (hiring) and removing (firing) a few items.
Creating a Model
There are three ways to create a list model:
- DefaultListModel — everything is pretty much taken care of for you. The examples in this page use
DefaultListModel
.- AbstractListModel — you manage the data and invoke the "fire" methods. For this approach, you must subclass
AbstractListModel
and implement thegetSize
andgetElementAt
methods inherited from theListModel
interface.- ListModel — you manage everything.
Initializing a List
Here is the code fromListDialog.java
that creates and sets up its list:The code passes an array to the list's constructor. The array is filled with strings that were passed in from another object. In our example, the strings happen to be boys' names.list = new JList(data); //data has type Object[]
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
list.setVisibleRowCount(-1);
...
JScrollPane listScroller = new JScrollPane(list);
listScroller.setPreferredSize(new Dimension(250, 80));Other
JList
constructors let you initialize a list from aVector
or from an object that adheres to theListModel
interface. If you initialize a list with an array or vector, the constructor implicitly creates a default list model. The default list model is immutable — you cannot add, remove, or replace items in the list. 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 ofDefaultListModel
. You can set a list's model when you create the list or by calling thesetModel
method. See Adding Items to and Removing Items from a List for an example.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.The call to
setLayoutOrientation
lets the list display its data in multiple columns. The valueJList.HORIZONTAL_WRAP
specifies that the list should display its items from left to right before wrapping to a new row. Another possible value isJList.VERTICAL_WRAP
, which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column. The following figures show these two wrapping possibilities, together with the default,JList.VERTICAL
.
HORIZONTAL_WRAP
VERTICAL_WRAP
VERTICAL
In combination with the call to
setLayoutOrientation
, invokingsetVisibleRowCount(-1)
makes the list display the maximum number of items possible in the available space onscreen. Another common use ofsetVisibleRowCount
is to specify to the lists's scroll pane how many rows the list prefers to display.
Tidak ada komentar:
Posting Komentar