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.

import java.awt.*;
import java.awt.event.*;
public class SuperBazarBilling extends Frame implements ActionListener
{
  List bazar;
  double rates[] = { 80.50, 75.60, 150.25, 39.99, 120.25, 90.80, 50.00 };
  public SuperBazarBilling()
  {                          // create List object
    bazar = new List(5, true);
                              // populate list
    bazar.add("Gloves");            
    bazar.add("Socks");
    bazar.add("Waist Belt");
    bazar.add("Cap");
    bazar.add("Wallet");
    bazar.add("Pen Set");
    bazar.add("Napkins");
                               // register with listener
    bazar.addActionListener(this);
                            // window closing using adapter
    PleaseClose pc = new PleaseClose();
    addWindowListener(pc);
                             // add to frame
    add(bazar, "North");
                            // frame creation trio
    setTitle("Scrollable List");
    setSize(300, 350);
    setVisible(true);
  }                // override the abstract method of AL
  public void actionPerformed(ActionEvent e)
  {
    repaint();
  }
  public void paint(Graphics g)
  {
    String itemNames[] = bazar.getSelectedItems();
    int itemIndexes[] = bazar.getSelectedIndexes();
      
    int y = 170;
    double totalAmount = 0.0;	
    g.drawString("Sl.No.            Item Name    Item code        Rate", 50, 150);
    for(int i = 0; i < itemNames.length; i++,  y  +=  25)   
    {
      g.drawString((i + 1) + "  " + itemNames[i] + "  " + itemIndexes[i] + "  "  + rates[itemIndexes[i]], 50, y);
      totalAmount  +=  rates[ itemIndexes[i]];
    }  
    g.setColor(Color.blue);
    g.drawString("Total Bill Rs." + totalAmount, 50, y  +=  25);
    g.setColor(Color.magenta);
    g.drawString("Thnaks Visit Again.", 50,  y +=  25);
    g.setColor(Color.red);
    g.drawString("Comments Book Available", 50,  y +=  25);
  }
  public static void main(String args[])
  {
    new SuperBazarBilling();
  }
}

class PleaseClose extends WindowAdapter
{
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
}

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

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.