Java AWT List Multiple Single Selection

Java AWT List Multiple Single Selection


List permits the selection of multiple items (Choice allows single selection). Programmer prefers List when checkboxes are many to display (in case of Choice, when radio buttons are many to display). List adds a scrollbar with which items can be scrolled and seen. List can be designed for single selection or multiple selection (Java AWT List Multiple Single Selection).

In general, all GUI components generate only one event. But in contrary, List can generate two events. For single clicks, it generates ItemEvent handled by ItemListener and for double clicks, ActionEvent handled by ActionListener. These two types of clicks can be used as in Window Explorer; single click selects the file and double click opens the file.

Choice Vs. List

Following table gives clear cut differences with Choice.

Choice List
Used for single selection only Used for single and multiple selections
Generally gives dropdown list Gives scrollable list of items
Generally, scrollbar is not required Generally scrollbar is seen
Generates only one event – ItemEvent Generates two events – ItemEvent and ActionEvent
Replacement to radio buttons Replacement to radio buttons or checkboxes

Following is the class signature

public class List extends Component implements ItemSelectable, Accessible

Example on Java AWT List – Preparing Super Bazar Bill

We have seen earlier in Choice component, the creation of applets with GUI. This program is also meant to practice GUI with applets. As a practice, you can change this program into an application. In the following applet, the items, selected by the user, in a super bazar are tabulated and made in a bill format.

The class implements ActionListener and thereby double clicks on the items only work.

3 thoughts on “Java AWT List Multiple Single Selection

  1. S. Nageswara Rao, Corporate Trainer Post author

    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 Reply

Your email address will not be published. Required fields are marked *