other

Senin, 01 Desember 2008

HOW TO USE LIST

A 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:

  1. 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.

    Launches the ListDemo example

  2. Click the Launch button to run ListDialogRunner. Alternatively, to compile and run the example yourself, consult the example index.

    Launches the ListDialogRunner example

  3. 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.
  4. 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 the getSize and getElementAt methods inherited from the ListModel interface.
  • ListModel — you manage everything.

Initializing a List

Here is the code from ListDialog.java that creates and sets up its list:
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));
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.

Other JList constructors let you initialize a list from a Vector or from an object that adheres to the ListModel 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 of DefaultListModel. You can set a list's model when you create the list or by calling the setModel 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 value JList.HORIZONTAL_WRAP specifies that the list should display its items from left to right before wrapping to a new row. 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 following figures show these two wrapping possibilities, together with the default, JList.VERTICAL.

HORIZONTAL_WRAP VERTICAL_WRAP VERTICAL
HORIZONTAL_WRAP VERTICAL_WRAP VERTICAL

In combination with the call to setLayoutOrientation, invoking setVisibleRowCount(-1) makes the list display the maximum number of items possible in the available space onscreen. Another common use of setVisibleRowCount is to specify to the lists's scroll pane how many rows the list prefers to display.

Tidak ada komentar:

other