Java AWT List Multiple Single Selection
bazar = new List(5, true);
The list bazar is set to multiple mode with the second parameter true. True indicates multiple selection and false indicates single selection. 5 indicates the visible items in the list at a time; to see the other than 5, scrollbar may be used.
The List object bazar is populated with items using add() method.
String itemNames = bazar.getSelectedItems();
int itemIndexes = bazar.getSelectedIndexes();
getSelectedItems() method returns all the items selected by the user as a string array and getSelectedIndexes() returns the index numbers of items selected by the user as an integer array. An array, rates, is used to prepare a bill of the items selected.
The paint() method is called implicitly in two circumstances – first time when the frame is created or when the frame is resized by dragging the sides. If the programmer would like to call the paint() method, he should call repaint() method but not paint() method directly (if called, it is a compilation error).
As illustrated in the above figure, the repaint() method calls implicitly update() method and again update() method calls implicitly the paint() method. The update() method clears the earlier drawings existing on the frame and on the cleared surface, the paint() method draws afresh the latest drawings.
Note: WindowAdapter usage, to close the window, is given in "Java Frame Closing – WindowAdapter"
Some more frequently used methods with List not used in the above program.
- int getItemCount() : Returns the number of items present in the list.,
- String getItem(int index) : Returns the item at the specified index.
- void add(String item, int index) : Inserts the item at the specified index. The original item is pushed to the next. By default, the items added in the list are given index numbers – the first one being zero, the second as one and so on.
- void replaceItem(String newItem, int index) : Replaces the item at the specified index with the specified one, newItem. The original one is lost.
- void remove(String item) : Removes the specified item. Item is specified as a string.
- void remove(int index) : Removes the specified item. Item is specified with index number.
- int getSelectedIndex() : Returns the index number of the item selected by the user. Used when ItemListener is implemented.
- String getSelectedItem() : Returns the item name as string selected by the user. Used when ItemListener is implemented.
- void select(int index) : By default the item at the specified index is selected.
- int getRows() : Returns the number of visible items in the list.
- void addItemListener(ItemListener il) : Registers the list with the item listener. Now the single clicks of the list are handled by item listener.
Methods like select(), addItemListener(), getSelectedItem() and getSelectedIndex() etc. are used in "Java AWT Choice – Replacement to Radio Button".
The internal process of what happens at a button click is discussed in Event Handling.