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.
Visual bean basics
Using VisualAge beans in visual composition
Adding the IBM Java Examples project
List and slider beans
BookmarkList sample
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:
void initializeChoices(choiceType myChoices);
Specify the appropriate class for the choiceType:
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);
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.
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:
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:
void initializeChoices(choiceType myChoices);
Specify the appropriate class for the choiceType:
myChoices.addItem("East"); myChoices.addItem("West"); myChoices.addItem("South"); myChoices.addItem("North");
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.
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.
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.