SlideShare a Scribd company logo
1 of 7
LISTS
USING LISTS
• The List class provides a compact, multiple-choice, scrolling
selection list.
• Unlike the Choice object, which shows only the single selected item
in the menu, a List object can be constructed to show any number
of choices in the visible window.
• It can also be created to allow multiple selections. List provides
these constructors:
‐ List( ) throws HeadlessException
‐ List(int numRows) throws HeadlessException
‐ List(int numRows, boolean multipleSelect) throws HeadlessException
• The first version creates a List control that allows only one item to
be selected at any one time.
• In the second form, the value of numRows specifies the number of
entries in the list that will always be visible (others can be scrolled
into view as needed).
• In the third form, if multipleSelect is true, then the user may select
two or more items at a time. If it is false, then only one item may be
selected.
• To add a selection to the list, call add( ). It has the following two forms:
‐ void add(String name)
‐ void add(String name, int index)
• Here, name is the name of the item added to the list. The first form adds
items to the end of the list.
• The second form adds the item at the index specified by index. Indexing
begins at zero. You can specify –1 to add the item to the end of the list.
• For lists that allow only single selection, you can determine which item is
currently selected by calling either getSelectedItem( ) or
getSelectedIndex( ). These methods are shown here:
‐ String getSelectedItem( )
‐ int getSelectedIndex( )
• The getSelectedItem( ) method returns a string containing the name of
the item.
• If more than one item is selected, or if no selection has yet been made,
null is returned.
• getSelectedIndex( ) returns the index of the item.
• The first item is at index 0. If more than one item is selected, or if no
selection has yet been made, –1 is returned.
• For lists that allow multiple selection, you must use either
getSelectedItems( ) or getSelectedIndexes( ), shown here, to
determine the current selections:
‐ String[ ] getSelectedItems( )
‐ int[ ] getSelectedIndexes( )
• getSelectedItems( ) returns an array containing the names of
the currently selected items.
• getSelectedIndexes( ) returns an array containing the indexes
of the currently selected items.
• To obtain the number of items in the list, call getItemCount( ).
You can set the currently selected item by using the select( )
method with a zero-based integer index. These methods are
shown here:
‐ int getItemCount( )
‐ void select(int index)
• Given an index, you can obtain the name associated with the
item at that index by calling getItem( ), which has this general
form:
‐ String getItem(int index)
• Here, index specifies the index of the desired item.
HANDLING LISTS
• To process list events, you will need to implement the
ActionListener interface.
• Each time a List item is double-clicked, an ActionEvent object is
generated.
• Its getActionCommand( ) method can be used to retrieve the name
of the newly selected item.
• Also, each time an item is selected or deselected with a single click,
an ItemEvent object is generated.
• Its getStateChange( ) method can be used to determine whether a
selection or deselection triggered this event.
• getItemSelectable( ) returns a reference to the object that triggered
this event.
Here is an example that converts the Choice controls in the preceding section into
List components, one multiple choice and the other single choice:
The output of this program is
THANK YOU

More Related Content

What's hot

Tree user interaction
Tree user interactionTree user interaction
Tree user interactionHemakumar.S
 
Excel Workshop
Excel WorkshopExcel Workshop
Excel WorkshopNhi Tran
 
Getting Started With XLSIM
Getting Started With XLSIMGetting Started With XLSIM
Getting Started With XLSIManalycorp
 
-WORKIN WITH EXCEL DATA
-WORKIN WITH EXCEL DATA -WORKIN WITH EXCEL DATA
-WORKIN WITH EXCEL DATA sonalijain1970
 
multiple linear regression in spss (procedure and output)
multiple linear regression in spss (procedure and output)multiple linear regression in spss (procedure and output)
multiple linear regression in spss (procedure and output)Unexplord Solutions LLP
 
Java Tutorial Lab 7
Java Tutorial Lab 7Java Tutorial Lab 7
Java Tutorial Lab 7Berk Soysal
 
Formulas in ms excel for statistics(report2 in ict math ed)
Formulas in ms excel for statistics(report2 in ict math ed)Formulas in ms excel for statistics(report2 in ict math ed)
Formulas in ms excel for statistics(report2 in ict math ed)Caryl Mae Puertollano
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manualsrv1972
 
Menu Driven Hospital Interface Desciption
Menu Driven Hospital Interface DesciptionMenu Driven Hospital Interface Desciption
Menu Driven Hospital Interface DesciptionAkshay Sharma
 
Using trace arrows in Excel
Using trace arrows in ExcelUsing trace arrows in Excel
Using trace arrows in ExcelDave Appleby
 
Executing tools-in-modelbuilder-tutorial
Executing tools-in-modelbuilder-tutorialExecuting tools-in-modelbuilder-tutorial
Executing tools-in-modelbuilder-tutorialaskar_gila
 

What's hot (19)

Arrays
ArraysArrays
Arrays
 
Tree user interaction
Tree user interactionTree user interaction
Tree user interaction
 
24option
24option24option
24option
 
Excel Workshop
Excel WorkshopExcel Workshop
Excel Workshop
 
Standard algorithms
Standard algorithmsStandard algorithms
Standard algorithms
 
Getting Started With XLSIM
Getting Started With XLSIMGetting Started With XLSIM
Getting Started With XLSIM
 
-WORKIN WITH EXCEL DATA
-WORKIN WITH EXCEL DATA -WORKIN WITH EXCEL DATA
-WORKIN WITH EXCEL DATA
 
E views tutorial
E views tutorialE views tutorial
E views tutorial
 
multiple linear regression in spss (procedure and output)
multiple linear regression in spss (procedure and output)multiple linear regression in spss (procedure and output)
multiple linear regression in spss (procedure and output)
 
Advanced Excel, Day 1
Advanced Excel, Day 1Advanced Excel, Day 1
Advanced Excel, Day 1
 
Add-subtract-using-excel
Add-subtract-using-excelAdd-subtract-using-excel
Add-subtract-using-excel
 
Java Tutorial Lab 7
Java Tutorial Lab 7Java Tutorial Lab 7
Java Tutorial Lab 7
 
Formulas in ms excel for statistics(report2 in ict math ed)
Formulas in ms excel for statistics(report2 in ict math ed)Formulas in ms excel for statistics(report2 in ict math ed)
Formulas in ms excel for statistics(report2 in ict math ed)
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
 
Menu Driven Hospital Interface Desciption
Menu Driven Hospital Interface DesciptionMenu Driven Hospital Interface Desciption
Menu Driven Hospital Interface Desciption
 
XL-MINER:Partition
XL-MINER:PartitionXL-MINER:Partition
XL-MINER:Partition
 
Using trace arrows in Excel
Using trace arrows in ExcelUsing trace arrows in Excel
Using trace arrows in Excel
 
List view1
List view1List view1
List view1
 
Executing tools-in-modelbuilder-tutorial
Executing tools-in-modelbuilder-tutorialExecuting tools-in-modelbuilder-tutorial
Executing tools-in-modelbuilder-tutorial
 

Similar to Lists

Basic controls in asp
Basic controls in aspBasic controls in asp
Basic controls in aspSireesh K
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletraksharao
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eGina Bullock
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eGina Bullock
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfalivaisi1
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptxSruthyPJ
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docxajoy21
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptxOut Cast
 
Question 1 MiniList collection 20 marks In this question .pdf
Question 1 MiniList collection 20 marks In this question .pdfQuestion 1 MiniList collection 20 marks In this question .pdf
Question 1 MiniList collection 20 marks In this question .pdfabhisheksharmasre
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxssuser10ef65
 
UNIT 5 list controls.pptx
UNIT 5 list controls.pptxUNIT 5 list controls.pptx
UNIT 5 list controls.pptxDivyak978237
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionssuseredfbe9
 
Cso gaddis java_chapter13
Cso gaddis java_chapter13Cso gaddis java_chapter13
Cso gaddis java_chapter13mlrbrown
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysSyed Afaq Shah MACS CP
 

Similar to Lists (20)

Basic controls in asp
Basic controls in aspBasic controls in asp
Basic controls in asp
 
java-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of appletjava-Unit4 chap2- awt controls and layout managers of applet
java-Unit4 chap2- awt controls and layout managers of applet
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Eo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5eEo gaddis java_chapter_12_5e
Eo gaddis java_chapter_12_5e
 
Built-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdfBuilt-in Data Structures in python 3.pdf
Built-in Data Structures in python 3.pdf
 
COLLECTIONS.pptx
COLLECTIONS.pptxCOLLECTIONS.pptx
COLLECTIONS.pptx
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
Lists
ListsLists
Lists
 
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptxRANDOMISATION-NUMERICAL METHODS  FOR ENGINEERING.pptx
RANDOMISATION-NUMERICAL METHODS FOR ENGINEERING.pptx
 
Question 1 MiniList collection 20 marks In this question .pdf
Question 1 MiniList collection 20 marks In this question .pdfQuestion 1 MiniList collection 20 marks In this question .pdf
Question 1 MiniList collection 20 marks In this question .pdf
 
javacollections.pdf
javacollections.pdfjavacollections.pdf
javacollections.pdf
 
Unit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptxUnit – I-AWT-updated.pptx
Unit – I-AWT-updated.pptx
 
UNIT 5 list controls.pptx
UNIT 5 list controls.pptxUNIT 5 list controls.pptx
UNIT 5 list controls.pptx
 
oop lecture framework,list,maps,collection
oop lecture framework,list,maps,collectionoop lecture framework,list,maps,collection
oop lecture framework,list,maps,collection
 
List in java
List in javaList in java
List in java
 
Collection Framework-1.pptx
Collection Framework-1.pptxCollection Framework-1.pptx
Collection Framework-1.pptx
 
Cso gaddis java_chapter13
Cso gaddis java_chapter13Cso gaddis java_chapter13
Cso gaddis java_chapter13
 
Lecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arraysLecture 7- Iterator and for loop over arrays
Lecture 7- Iterator and for loop over arrays
 
Collections and generics
Collections and genericsCollections and generics
Collections and generics
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 

Recently uploaded

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrandmasabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Hararemasabamasaba
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 

Recently uploaded (20)

WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 

Lists

  • 2. USING LISTS • The List class provides a compact, multiple-choice, scrolling selection list. • Unlike the Choice object, which shows only the single selected item in the menu, a List object can be constructed to show any number of choices in the visible window. • It can also be created to allow multiple selections. List provides these constructors: ‐ List( ) throws HeadlessException ‐ List(int numRows) throws HeadlessException ‐ List(int numRows, boolean multipleSelect) throws HeadlessException • The first version creates a List control that allows only one item to be selected at any one time. • In the second form, the value of numRows specifies the number of entries in the list that will always be visible (others can be scrolled into view as needed). • In the third form, if multipleSelect is true, then the user may select two or more items at a time. If it is false, then only one item may be selected.
  • 3. • To add a selection to the list, call add( ). It has the following two forms: ‐ void add(String name) ‐ void add(String name, int index) • Here, name is the name of the item added to the list. The first form adds items to the end of the list. • The second form adds the item at the index specified by index. Indexing begins at zero. You can specify –1 to add the item to the end of the list. • For lists that allow only single selection, you can determine which item is currently selected by calling either getSelectedItem( ) or getSelectedIndex( ). These methods are shown here: ‐ String getSelectedItem( ) ‐ int getSelectedIndex( ) • The getSelectedItem( ) method returns a string containing the name of the item. • If more than one item is selected, or if no selection has yet been made, null is returned. • getSelectedIndex( ) returns the index of the item. • The first item is at index 0. If more than one item is selected, or if no selection has yet been made, –1 is returned.
  • 4. • For lists that allow multiple selection, you must use either getSelectedItems( ) or getSelectedIndexes( ), shown here, to determine the current selections: ‐ String[ ] getSelectedItems( ) ‐ int[ ] getSelectedIndexes( ) • getSelectedItems( ) returns an array containing the names of the currently selected items. • getSelectedIndexes( ) returns an array containing the indexes of the currently selected items. • To obtain the number of items in the list, call getItemCount( ). You can set the currently selected item by using the select( ) method with a zero-based integer index. These methods are shown here: ‐ int getItemCount( ) ‐ void select(int index) • Given an index, you can obtain the name associated with the item at that index by calling getItem( ), which has this general form: ‐ String getItem(int index) • Here, index specifies the index of the desired item.
  • 5. HANDLING LISTS • To process list events, you will need to implement the ActionListener interface. • Each time a List item is double-clicked, an ActionEvent object is generated. • Its getActionCommand( ) method can be used to retrieve the name of the newly selected item. • Also, each time an item is selected or deselected with a single click, an ItemEvent object is generated. • Its getStateChange( ) method can be used to determine whether a selection or deselection triggered this event. • getItemSelectable( ) returns a reference to the object that triggered this event.
  • 6. Here is an example that converts the Choice controls in the preceding section into List components, one multiple choice and the other single choice: The output of this program is