by Dinesh Thakur

List displays a list of items. It allows the user to make multiple selections from the given list of items. The list component is added in a scroll pane, so that the long list of items is scrollable. A list is an object of JList class.

Some of the constructors defined by JList class are as follows.

JList ()

JList(Object[] listdata)

where,

listdata represents the array of Object type that displays the elements

 

import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;
public class JavaExampleMultiSelectInApplet extends Applet implements ActionListener
     {
        List Lst;
        TextField Txt;
        Button BtnShw;
        String Slctns[];
        public void init(){
        Txt = new TextField(40);
        add(Txt);
        Lst = new List(4,true);
        Lst.add("Item One");
        Lst.add("Item two");
        Lst.add("Item three");
        Lst.add("Item Four");
        Lst.add("Item five");
        Lst.add("Item Six");
        Lst.add("Item Seven");
        Lst.add("Item Nine");
        Lst.add("Item Ten");
        add(Lst);
        BtnShw = new Button("Show Selections");
        BtnShw.addActionListener(this);
        add(BtnShw);
     }
        public void actionPerformed(ActionEvent e1)
         {
           String OutStrng = new String("You Choose:");
           if(e1.getSource() == BtnShw)
             {
                 Slctns = Lst.getSelectedItems();
                for(int loopIndx = 0; loopIndx < Slctns.length; loopIndx++)
                {
                       OutStrng += " " + Slctns[loopIndx];
                    }
                       Txt.setText(OutStrng);
             }
         }
 }
/*<APPLET  CODE=JavaExampleMultiSelectInApplet.class WIDTH=300  HEIGHT=200 ></APPLET>*/

Java - List - Select Multiple items Example