You are on page 1of 4

Exp- 2

1) State difference between List and Choice Control.

Ans.

A Choice is displayed in a compact form that requires you to pull it down to see
the list of available choices. Only one item may be selected from a Choice. Choice
is the act of picking or deciding between two or more possibilities. Only one item
may be selected from a choice.

A List may be displayed in such a way that several List items are visible. A List
supports the selection of one or more List items. A list is any enumeration of a set
of items. A List supports the selection of one or more list items.

Q.2) Write use of getSelectedItem() and getselectedIndex() for list.

Ans.

1) The getSelectedItem() method returns the label of the selected item. The
label is the string used in the add() or addItem() call. If nothing is selected in
the List, getSelectedItem() returns null. The return value is also null if List is
in multiselect mode and multiple items are selected.

2) The getSelectedIndex() method returns the position of the selected item. If


nothing is selected in the List, getSelectedIndex() returns -1. The value -1 is
also returned if the List is in multiselect mode and multiple items are
selected. For multiselection lists, use getSelectedIndexes() instead
Program:-

Program to design a form using components List and Choice.


import java.awt.*;

import java.applet.*;

public class listandchoice extends Applet

public void init()

Label lb=new Label("List and Choice Program");

List L1=new List(4);

List L2=new List(3,true);

Choice c1=new Choice();

Choice c2=new Choice();

L1.add("Europe");

L1.add("Asia");

L1.add("North America");

L1.add("Australia");

L2.add("India");

L2.add("England");

L2.add("USA");

L2.add("Australia");

c1.add("Maharashra");

c1.add("Karnataka");

c1.add("Goa");

c2.add("Mumbai");
c2.add("Bangalore");

c2.add("Panaji");

c2.add("Pune");

c2.add("Mangalore");

add(lb);

add(L1);

add(L2);

add(c1);

add(c2);

/*<applet code="listandchoice"width="500"height="500">

</applet>*/
OUTPUT:

You might also like