Class 2 |
Intermediate Java 30-IT-397 |
|
Getting Selected Values
-
Most often, you'll want to find which items on the JList the user selected
in one of two ways:
-
When the user selects an item, you can find the selected item and perform
operations based on the selection. For example, if the user selects
513 from a list of area codes, you might default the state to Ohio.
-
When the user clicks a button, such as add, submit, next, etc. You
can get the selected value from the JList and save it to a database, or
some type of collection object for future processing.
-
To get the selected items from the JList, we use the getSelectedValues()
method of the JList object.
-
This returns an object array. If we just put Strings in the JList,
we can iterate through this array and cast each element to a String object
when we need to use it.
-
If you only permit the user to select one item, you can use the getSelectedValue()
method to return that one selection as an object (not an object array).
Then, you can cast that object to a more specific object for use.
Example: Using Strings, the Old Fahioned Way

Created by: Brandan Jones
December 17, 2001