Drop-down lists | CodeGuru

Drop-down lists

Bruce Eckel’s Thinking in Java Contents | Prev | Next Like a group of radio buttons, a drop-down list is a way to force the user to select only one element from a group of possibilities. However, it’s a much more compact way to accomplish this, and it’s easier to change the elements of the […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 1, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Like


a group of radio buttons, a

drop-down
list is a way to force the user to select only one element from a group of
possibilities. However, it’s a much more compact way to accomplish this,
and it’s easier to change the elements of the list without surprising the
user. (You can change radio buttons dynamically, but that tends to be visibly
jarring).

Java’s


Choice
box is not like the combo box in Windows, which lets you select from a list
or
type in your own selection. With a
Choice
box you choose one and only one element from the list. In the following
example, the
Choice
box starts with a certain number of entries and then new entries are added to
the box when a button is pressed. This allows you to see some interesting
behaviors in
Choice
boxes:
//: Choice1.java
// Using drop-down lists
import java.awt.*;
import java.applet.*;
 
public class Choice1 extends Applet {
  String[] description = { "Ebullient", "Obtuse",
    "Recalcitrant", "Brilliant", "Somnescent",
    "Timorous", "Florid", "Putrescent" };
  TextField t = new TextField(30);
  Choice c = new Choice();
  Button b = new Button("Add items");
  int count = 0;
  public void init() {
    t.setEditable(false);
    for(int i = 0; i < 4; i++)
      c.addItem(description[count++]);
    add(t);
    add(c);
    add(b);
  }
  public boolean action (Event evt, Object arg) {
    if(evt.target.equals(c))
      t.setText("index: " +  c.getSelectedIndex()
        + "   " + (String)arg);
    else if(evt.target.equals(b)) {
      if(count < description.length)
        c.addItem(description[count++]);
    }
    else
      return super.action(evt, arg);
    return true;
  }
} ///:~ 

The


TextField

displays the “selected index,” which is the sequence number of the


currently selected element, as well as the


String

representation of the second argument of


action( )

,


which is in this case the string that was selected.

When


you run this applet, pay attention to the determination of the size of the


Choice

box: in Windows, the size is fixed from the first time you drop down the list.


This means that if you drop down the list, then add more elements to the list,


the elements will be there but the drop-down list won’t get any longer


[57]

(you can scroll through the elements). However, if you add all the elements


before the first time the list is dropped down, then it will be sized


correctly. Of course, the user will expect to see the whole list when


it’s dropped down, so this behavior puts some significant limitations on


adding elements to


Choice

boxes.



[57]

This behavior is apparently a bug and will be fixed in a later version of Java.

Contents

|

Prev

|

Next
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.