Way2Java

Java Choice Replacement Radio Buttons

Choice, another GUI component from the toolkit of classes of java.awt. Choice presents a pull-down menu. Choice is a replacement to radio buttons. Choice is chosen when radio buttons are many to display. Choice is popularly known as Combo box in other GUI environments. Choice, like radio buttons, allows selecting only one option among many. Choice occupies less space on the container than many radio buttons doing the same job. Many radio buttons flock the whole container and in this case alternative to the programmer is Choice. When the user selects some item, the current selected item is displayed and the drop down list disappears.

Choice generates ItemEvent and handled by ItemListener. Following is the class signature of Choice component.

public class Choice extends Component implements ItemSelectable, Accessible

This is the second program developed as an applet instead of frame; the first one being radio buttons. This attempt is only for practicing applets with GUI.

Example on Java Choice Replacement Radio Buttons
import java.applet.Applet;
import java.awt.*;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class VisitingPlaces extends Applet implements ItemListener
{
  Choice visit;
  TextField tf;
  String states[] = { "Tamilnadu", "Karnataka", "Andhra Pradesh", "Kerala", "Maharashtra" };
  public void init()
  {                            // set the layout
    setLayout(new BorderLayout());
                               // create the components
    visit = new Choice();
    tf = new TextField(15);
                               // populate the Choice
    visit.add("Chennai");
    visit.add("Bengaluru");
    visit.add("Kochi");
    visit.add("Mumbai");
    visit.addItem("Kolkota");
                            // learning some more methods
    visit.insert("Hyderabd", 2);
    visit.select("Kochi");
    visit.remove("Kolkota");
                           // register with the listener
    visit.addItemListener(this);
                           // add the components to applet
    add(visit, BorderLayout.NORTH);
    add(tf, BorderLayout.SOUTH);
  }
  public void paint(Graphics g)
  {
    g.drawString("No. of items: " + visit.getItemCount(), 50, 150);
    g.drawString("Item at 2nd index number: " + visit.getItem(2), 50, 170);
   }
   public void itemStateChanged(ItemEvent e)
   {              // get the item selected by the user
     String str = visit.getSelectedItem();
     int x = visit.getSelectedIndex();
                  // dispaly in the text field
     tf.setText("U go to " + str + " of state " + states[x]);
   }
}

HTML file to run the above applet
FileName: Visit.html

     
     


Output of Java Choice Replacement Radio Buttons

Java Choice Replacement Radio Buttons

setLayout(new BorderLayout());

The default layout manager for Applet is FlowLayout and it is changed to BorderLayout in the program.

visit.add("Mumbai");
visit.addItem("Kolkota");

Two methods add() and addItem() exist with Choice to add the items. But addItem() became obsolete from JDK 1.1 (not deprecated). It is advised to use add() method.

visit.insert("Hyderabd", 2);
visit.select("Kochi");
visit.remove("Kolkota");

The Choice component maintains an array to store the items added. The first item added Chennai gets an index number of 0 by default and so on. The insert() method inserts an item in between the existing items. The original items after the index number inserted are pushed down by one. By default, the first item added is displayed in the choice. The select("Kochi") displays Kochi by default. The remove() method removes Kolkota from the items.

add(visit, BorderLayout.NORTH);
add(tf, BorderLayout.SOUTH);

The components are added to the applet with BorderLayout.NORTH instead of conventional "North".

String str = visit.getSelectedItem();
int x = visit.getSelectedIndex();

getSelectedItem() returns the item selected by the user as a string and getSelectedIndex() returns the index number of the items selected. This data is displayed in the text field. The states[] array is used to display the visiting places capitals. Note: Step-by-step explanation of event handling is available in "AWT Button – Learning GUI – 8 Steps".