Creating Choice Controls in an Applet Window using Advanced Java Programming

Home  »  Java  »  Advance Java »    Java AWT (Abstract Window ToolKit) »    Choice Controls

These are same as a Simple List Which contains a Series of Choices and one can select an item from that list . But theses Choice Lists provides selection of only one item from a list and to add various optisons we have to use the add Method of Lists like this.

Choice ch=new Choice(); // Create Object of Choice Class
Ch.add("Red");// add : Name of Method to Add Options in List.
Ch.add("Green");
Ch.add("Yellow");
add(Ch);

The first statement creates an object of the Choice class.

The second and third statement adds two items to the Choice list.

The following example displays a dropdown Choice list in applet ChoiceTest.

The above code will be stored in the file called ChoiceTest.java, which on compilation will crate a class file called ChoiceTest.class. Now compiple this file using below commands in the DOS mode:

C:\>jdk1.4\bin>javac ChoiceTest.java
C:\>jdk1.4\bin>appletviewer ChoiceTest.java

"Output of ChoiceTest.java"


Download Complete Program

  -  



Methods of the Chioce Object:

Methods
Action
GetItem(int)Returns the string item at the given position(items inside a choice begins at 0,as in arrays).
GetSelectedIndex()Returns the index position of the item that is selected
GetSelectedItem()Returns the currently selected item as a string
Select(int)Selects the item at the given position.
Select(String)Selects the item with the given string.




AWT Controls



Label Buttons Check Boxes Radio Buttons
Choice Controls TextFields & Password TextAreas Scrolling Lists Scrollbars