Adding a Choice Component using Java


0

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");
}
}



Like it? Share with your friends!

0

What's Your Reaction?

hate hate
0
hate
confused confused
0
confused
fail fail
0
fail
fun fun
0
fun
geeky geeky
0
geeky
love love
0
love
lol lol
0
lol
omg omg
0
omg
win win
0
win
Anand Narayanaswamy
Anand Narayanaswamy is the editor-in-chief of Learnxpress. He was a Microsoft Most Valuable Professional (MVP) for a period of 9 years. He is a ASPInsider based in Trivandrum, Kerala State, India. Anand is the author of Community Server Quickly published by Packt Publishing.

0 Comments

Your email address will not be published. Required fields are marked *