Adding a Choice Component using Java

In this code snippet, you will learn how to add a choice component using Java language

/*<applet code = "Mycombo.class" 
width = 250 height = 250></applet> */

//Compilation: javac Mycombo.java
//Execution: appletviewer Mycombo.java

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Mycombo extends Applet implements ItemListener
{
TextField t1;
Choice c1;

public void init()
{
c1 = new Choice();
c1.addItem("India");
c1.addItem("USA");
c1.addItemListener(this);
add(c1);

t1 = new TextField(25);
add(t1);
}

public void itemStateChanged(ItemEvent e)
{
String s = (String)e.getItem();
if(s == "India")
{
t1.setText("Capital is NewDelhi");
}
else if (s == "USA")
t1.setText("Capital is washington");
}
}

										

Leave a Comment