Turn Desktop View Off
You are here:   HomeJava ProgrammingSwingJava - List - Select Multiple items Example
by Dinesh Thakur Category: Swing

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





Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article