Home arrow Java & J2EE arrow Page 7 - Java's Basic User Interface Components

The Choice List Class - Java

To simplify user interaction and make data entry easier, you can use java controls. Controls are components, such as buttons, labels and text boxes, that can be added to containers like frames, panels and applets. The Java.awt package provides an integrated set of classes to manage user interface components.

  1. Java's Basic User Interface Components
  2. Adding a Control to a Container
  3. The Button Class
  4. The Label Class
  5. The Checkbox Class
  6. The CheckboxGroup Class
  7. The Choice List Class
  8. The TextField and TextArea Class
  9. Setting a Password Character
  10. TextArea
By: Gayathri Gokul
Rating: starstarstarstarstar / 22
October 20, 2003

print this article


Choice Lists, which is created from the Choice class, are components that enable a single item to be picked from a pull-down list. We encounter this control very often on the web when filling out forms. The first step in creating a Choice List is to create a choice object to hold the list, as shown below:

Choice cgender = new Choice();

Items are added to the Choice List by using addItem(String) method on the object. The following code adds two items to the gender choice list.

cgender.addItem(“Female”); cgender.addItem(“Male”);

After you add the Choice List it is added to the container like any other component using the add() method. The following example shows an Applet that contains a list of shopping items in the store.

<Applet code= “ChoiceTest.class”
Width = 200
Height = 150>

Import java.awt.*;

Public class ChoiceTest extends java.applet.Applet
Choice shoplist = new Choice();

Public void init(){
shoplist.addItem(“Bed And Bath”);
shoplist.addItem(“Home Appliance”);
shoplist.addItem(“Toys and Accessories”);


The choice list class has several methods, which are given below:

Method Action
getItem() Returns the string item at the given position (items inside a choice begin at 0, just like arrays)
countItems() Returns the number of items in the menu
getSelectedIndex() Returns the index position of the item that's 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

>>> More Java & J2EE Articles          >>> More By Gayathri Gokul

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: