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".

3 thoughts on “Java Choice Replacement Radio Buttons”

Leave a Comment

Your email address will not be published.