Adding a list or slider component

List components provide a list of items for the user to select. Slider components show a range of selection values or show progress for the duration of an operation. VisualAge provides list and slider beans from the com.sun.java.swing and java.awt packages.

You can add a list or slider bean to enable the user to select an item or value.


Bean Description
JComboBox (Swing) or Choice (AWT) A selectable list with an entry field
JList (Swing) or List (AWT) A selectable list of choices
JProgressBar (Swing) A progress indicator
JScrollBar (Swing) or Scrollbar (AWT) A scrolling component
JSlider (Swing) A selection component for a range of values

Obtaining the selected choice or value

Connect a property representing the selection to a target property. Then, open properties for the connection and select a source event that indicates when the selection changes.
Bean Source property Source event
JComboBox selectedItem itemStateChanged
Choice selectedItem itemStateChanged
JList selectedValue or selectedValues valueChanged
List selectedItem or selectedItems itemStateChanged
JProgressBar value stateChanged
JSlider value stateChanged
JScrollBar value adjustmentValueChanged
ScrollBar value adjustmentValueChanged

To get the value of the selected choice, connect the Choice selectedItem property to the value target. To get the index of the selected choice, connect the Choice selectedIndex property to the value target.

Calling a method when the value changes

To call a method when the value changes, connect a source event that indicates when the selection changes to the method.

Bean--specific tasks

For examples, see the BookmarkList class in the com.ibm.ivj.examples.vc.swing.bookmarklist and com.ibm.ivj.examples.vc.bookmarklist packages, the ToDoList class in the com.ibm.ivj.examples.vc.todolist package, and the Amortization class in the com.ibm.ivj.examples.vc.mortgageamortizer package. These examples are shipped in the IBM Java Examples project.


Related procedures
Visual bean basics
Using VisualAge beans in visual composition
Adding the IBM Java Examples project

Related references
List and slider beans
BookmarkList sample

JList and List bean tasks

Making a JList bean scrollable

An AWT List implements scrolling, duplicating the capability of a ScrollPane. A JList does not implement scrolling itself, but uses the scrolling capability of a JScrollPane in which it is placed. If you want a JList to be scrollable, drop it in a JScrollPane.

Defining the selection mode for a JList or List bean

You can define a list to allow either a single selection or multiple selections. With single selection, the previous selection is deselected when the user selects another choice. Multiple selection differs between JList and List beans.

Defining choices

You can specify choices using an initialization method. For a JList, you can alternatively define the list in a ListModel. Add the list model class to the free-form surface. Then, connect the list model's this property to the JList's model property.

To specify the choices using an initialization method, follow these steps:

  1. After adding the bean, note its name. If you select the bean, its name appears in the Visual Composition Editor status area.

  2. Save your composite bean.

  3. On the Methods page, add a method to initialize the choices. The method signature should look like this:
    void initializeChoices(choiceType myChoices); 
    

    Specify the appropriate class for the choiceType:

  4. Enter code in the initialization method to add choices. For a List bean, use the addItem() method:
    myChoices.addItem("East");
    myChoices.addItem("West");
    myChoices.addItem("South");
    myChoices.addItem("North");
    

    For a JList bean, use the setListData() method:

    String[] data = {"East", "West", "South", "North"};
    myChoices.setListData(data); 
    

  5. Modify the get method for the bean you are initializing, for example, getJList1(). In user code block 1, add code to call the initialization method you just created. The method call should look like this:
    initializeChoices(instanceName); 
    

    Specify the instance name for the bean as the instanceName argument for the method call. The default instance name is something like ivjJList1, or ivjList1.

Obtaining the selected index or indexes for a JList or List bean

Connect the selectedIndex or selectedIndexes property to the value target. Then, open properties for the connection and select a source event that indicates when the selection changes.

JComboBox or Choice bean tasks

Getting the value that a user enters in a JComboBox bean

If you set the editable property of a JComboBox bean to True, the user can enter a value instead of selecting a choice from the list. To get an entered value, do the following:

  1. Tear off the editor property of the JComboBox bean.
  2. Tear off the editorComponent property of the torn-off editor property.
  3. Connect the keyReleased event of the torn-off editorComponent property to the property that is to receive the entered value.
  4. Connect the value parameter of the previous connection to the item property of the torn-off editor property.

Defining choices for a JComboBox or a Choice bean

You can specify choices using an initialization method.

To specify the choices using an initialization method, follow these steps:

  1. After adding the bean, note its name. If you select the bean, its name appears in the Visual Composition Editor status area.

  2. Save your composite bean.

  3. On the Methods page, add a method to initialize the choices. The method signature should look like this:
    void initializeChoices(choiceType myChoices); 
    

    Specify the appropriate class for the choiceType:

  4. Enter code in the initialization method to add choices. Use the addItem() method:
    myChoices.addItem("East");
    myChoices.addItem("West");
    myChoices.addItem("South");
    myChoices.addItem("North");
    

  5. Modify the get method for the bean you are initializing, for example, getJComboBox1(). In user code block 1, add code to call the initialization method you just created. The method call should look like this:
    initializeChoices(instanceName); 
    

    Specify the instance name for the bean as the instanceName argument for the method call. The default instance name is something like ivjJComboBox1 or ivjChoice1.

Allowing text entry in a JComboBox bean

Set the editable property to True in the property sheet.

Obtaining the selected index or indexes for a JComboBox or Choice

Connect the selectedIndex or selectedIndexes property to the value target. Then, open properties for the connection and select a source event that indicates when the selection changes.

JScroll and Scroll bean tasks

Defining the orientation of a JScrollBar or ScrollBar bean

Select a choice for the orientation property in the property sheet. The orientation choices are VERTICAL and HORIZONTAL.

Defining the value range for a JScrollBar or ScrollBar bean

Set the minimum and maximum properties in the property sheet.

Defining the initial value of a JScrollBar or ScrollBar bean

Set value property in the property sheet. The initial value determines the initial progress, selection, or scrolling position in the value range.

Defining scrolling increments for a JScrollBar or ScrollBar bean

To define the value change when the user clicks on a scroll arrow, set the unitIncrement property in the ScrollBar property sheet. To define the value change when the user clicks in the scroll bar range away from the scroll box, set the blockIncrement property.

JProgressBar and JSlider bean tasks

Defining the orientation of a JProgressBar or JSlider bean

Select a choice for the orientation property in the property sheet. The orientation choices are VERTICAL and HORIZONTAL.

Defining the value range for a JProgressBar or JSlider bean

Set the minimum and maximum properties in the property sheet.

Defining the initial value of a JProgressBar or JSlider bean

Set value property in the property sheet. The initial value determines the initial progress, selection, or scrolling position in the value range.

Defining tick marks or values for a JSlider bean

To define the value increment between tick marks, set the majorTickSpacing and minorTickSpacing properties in the JSlider property sheet. To automatically adjust a user selection to the closest tick mark, set the snapToTicks property to True. To show the tick marks, set the paintTicks property to True. To show the tick values, set the paintLabels property to True. To reverse the minimum and maximum ends of the slider, set the inverted property to True.

Setting the value of a JProgressBar bean

Connect the value to the JProgressBar value property. Then, open properties for the connection and select a source event that indicates when the selection changes.