Java AWT List Multiple Single Selection


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.

bazar.add("Gloves");
bazar.add("Socks");

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.

repaint() Method

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).

repaint() –> update() –> paint()

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.

  1. int getItemCount() : Returns the number of items present in the list.,
  2. String getItem(int index) : Returns the item at the specified index.
  3. 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.
  4. void replaceItem(String newItem, int index) : Replaces the item at the specified index with the specified one, newItem. The original one is lost.
  5. void remove(String item) : Removes the specified item. Item is specified as a string.
  6. void remove(int index) : Removes the specified item. Item is specified with index number.
  7. int getSelectedIndex() : Returns the index number of the item selected by the user. Used when ItemListener is implemented.
  8. String getSelectedItem() : Returns the item name as string selected by the user. Used when ItemListener is implemented.
  9. void select(int index) : By default the item at the specified index is selected.
  10. int getRows() : Returns the number of visible items in the list.
  11. 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.

3 thoughts on “Java AWT List Multiple Single Selection”

  1. It is given in the posting itself. paint() is called automatically in two circumstances a) when the first time frame is created and b) when the user resizes the frame by catching and dragging the border of the frame. If the programmer would like to call paint() method in the code, he should call repaint() and not paint().

    In the actionPerformed() we called repaint() method as the actual code of drawing on the frame exists in paint() method.

Leave a Comment

Your email address will not be published.