Concepts / List Models and Dynamic Lists

List Models and Dynamic Lists

A JList is built from an array whose objects become the items the list displays.

  • Programming

Predicting a Two-Click Selection

A JList is a Swing component that displays a set of selectable items. Many programs use one to let a user choose from a set of options, such as a row of icons. Before looking at how a JList is built, try to predict what happens in a plain, unconfigured list. Suppose the user clicks one icon, and then clicks a different icon in the same list. Think about what you expect to see selected before you read the answer.

What do you think happens?

In a JList with no further configuration, after you click icon A and then click a different icon B without holding any extra key, which icons are selected?

Reveal answer
Answer:

Only icon B is selected. A plain click selects a single item, so the second click takes over the selection from the first.

The source states that a single item is selected by clicking on it, and that several items are selected only by shift-clicking or control- or meta-clicking. Holding Shift or Ctrl (or Meta, depending on platform) is what lets a user keep several items selected at once.

The rest of this article answers two questions. First, how does the array of objects you pass in decide what the list shows? Second, how do the selection rules come from code you write, not from the list itself? Keep the prediction in mind, because it is the behavior you will trace later.

Building a List from an Array

A JList is built from an array. The objects stored in that array become the items the list displays. In the SillyStamper program, a Swing application, the images for the icons are read from resource files, placed into an array, and that array is then used to construct a JList of type Icon. The array is the source of the list's contents: whatever objects it holds are the objects the list is built to show.

java
Notes on the code
  • The array holds the objects that will be shown.
  • Passing the array to the JList constructor makes each array object an item in the list.
icons[0]first stamp iconlist index 0first stamp iconicons[1]second stamp iconlist index 1second stamp iconicons[2]third stamp iconlist index 2third stamp iconshown as itemshown as itemshown as item
Which array element ends up at which position in the displayed list, and how does an index in the list map back to an object in the array?

Read the diagram as a question you can ask of any JList you build. The left column is the array that was passed in; the right column is what the list displays. Each displayed item traces back to one array object. If an icon is missing or wrong on screen, the array that was passed to the constructor is the first place to check, before looking at anything that happens afterward. The selection rules, which come later, do not change which objects appear.

What a Default List Allows

Before changing anything, it helps to know what a JList does on its own. By default, the user can select any number of items in a list. The default gestures are listed below. The modifier key for the control-style gesture depends on the platform.

  • Click one item: selects that single item.
  • Shift-click: selects several items, as a range, with the Shift key held.
  • Control-click or meta-click: adds or removes individual items from the selection, with the modifier depending on the platform.
  • Default mode allows multiple selection, so the user can hold several items selected at the same time.

Restricting Selection with setSelectionMode

The SillyStamper program wanted only one icon to be chosen at a time. To impose that restriction, the program calls a method on the list that sets its selection mode. The call names a constant from the ListSelectionModel class, and the constant ListSelectionModel.SINGLE_SELECTION limits selection to one item at a time.

java
Notes on the code
  • Line 1 builds the list from the array, which decides what items appear.
  • Line 2 changes the selection rule on the existing list, which decides how many items the user can select.

Notice what the second line does and does not say. It does not build the list, and it does not choose which icon is selected. It changes the rule governing selection on a list that already exists. Because the list is constructed first and configured second, a failure can come from either step, so the two steps should be checked separately.

Plain clickselects one itemPlain clickselects one itemShift-clickselects a rangeShift-clickstill one itemControl-clickadds an itemControl-clickstill one itemsameno longer multipleno longer multiple
How does the selection behavior change before and after the single-selection mode is set, and what can the user no longer do?

The before-and-after comparison shows what the single-selection call removes. A plain click still selects one item. Shift-clicking and control-clicking, which in the default mode let the user build up several selected items, no longer produce a multiple selection once the mode is restricted to one item at a time.

Tracing an Unexpected Selection

When a JList behaves differently from what you expected, the goal is to find the line that caused it. The fastest method is to follow the order in which the program runs. The list is constructed from the array first, then its selection mode is set, and only then does user input reach the list. A flowchart of that order makes it easy to decide which line to inspect.

Build arraydecides which objects existConstruct JListdecides which items appearsetSelectionModedecides how many can beselectedUser clicksselection is applied underthe modepassed inthen configuredthen handled
In what order do the list setup, selection-mode call, and user event handling run, so that I can trace which line produced an unexpected selection?
Worked example

Diagnosing an unexpected two-icon selection

A developer expects only one stamp icon to be selectable, but the user can select two icons with Shift-click. Which line should be checked first?

  1. 1
    Check the array first

    Confirm the array holds the expected icon objects. Shift-click behavior does not depend on the array contents, so the array is not the cause of the selection problem, though it is the first place to check if an icon is missing.

  2. 2
    Check the construction line

    Constructing the JList from the array decides which items appear. It does not set any selection rule, so it cannot by itself restrict selection to one item.

  3. 3
    Check the selection-mode call

    The restriction must come from a call such as setSelectionMode(ListSelectionModel.SINGLE_SELECTION). If that call is missing, or sets a different constant, the list keeps its default multiple-selection behavior, which permits Shift-click ranges.

  4. 4
    Conclude

    The cause is the selection-mode line: the restricting call is absent or not applied, so the default mode is still in effect.

Result

The line responsible is the setSelectionMode call. Because the list is constructed first and configured second, the two steps are checked separately, and the configuration step is where a missing single-selection restriction shows up.

Mistakes That Hide the Real Cause

Several common errors make a JList look wrong when the real problem is in a different line. The items below are the ones most worth checking.

  • Common mistake

    Assuming the selection mode call decides which items appear

    Why it fails

    The source separates the two steps: construction from the array decides the contents, and the selection-mode call changes only the selection rule.

    Do this instead

    When an icon is missing, check the array and the construction line. When the selection count is wrong, check the setSelectionMode line.

  • Common mistake

    Expecting a plain JList to allow only one selection

    Why it fails

    A JList built with no further configuration accepts multiple selections by default.

    Do this instead

    Add setSelectionMode with ListSelectionModel.SINGLE_SELECTION if one-at-a-time selection is required.

  • Common mistake

    Fixing the array when the restriction is missing

    Why it fails

    Changing the objects in the array does not alter how many items the user may select.

    Do this instead

    Inspect the configuration line instead of rewriting the array.

  • Common mistake

    Forgetting that Shift-click and Control-click depend on the default mode and stop producing multiple selections under SINGLE_SELECTION

Practice Tracing a Selection

MEDIUM

A program builds a JList from an array of three icons and then calls setSelectionMode with ListSelectionModel.SINGLE_SELECTION. A user clicks the first icon, then Shift-clicks the third. Predict which icons are selected, then identify which line of code is responsible for that result.

Hints
  • Under single selection, only one item may be selected at a time.
  • The line that sets the mode is the configuration step, separate from the construction step that built the list from the array.

Key Takeaways from the Two Steps

  1. 1
    A JList is built from an array, and the objects in that array are the items the list displays.
  2. 2
    By default, a JList accepts multiple selections through clicking, shift-clicking, and control- or meta-clicking.
  3. 3
    Calling setSelectionMode with ListSelectionModel.SINGLE_SELECTION limits selection to one item at a time, and it changes the rule on an existing list rather than building it.
  4. 4
    When a JList misbehaves, check the construction from the array separately from the selection-mode configuration, and trace the order in which they run.

At a glance

  • A JList is built from an array whose objects become the items the list displays.
  • The default selection mode lets the user select multiple items through clicking, shift-clicking, and control- or meta-clicking.
  • Calling setSelectionMode with ListSelectionModel.SINGLE_SELECTION restricts selection to one item at a time.
  • When a JList misbehaves, check construction from the array separately from the selection-mode configuration.