Adding a Choice Component using Java

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

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">/*<span style="color: #0000ff">&lt;</span><span style="color: #800000">applet</span> <span style="color: #ff0000">code</span> = <span style="color: #0000ff">&quot;Mycombo.class&quot;</span> 
<span style="color: #ff0000">width</span> = <span style="color: #0000ff">250</span> <span style="color: #ff0000">height</span> = <span style="color: #0000ff">250</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">applet</span><span style="color: #0000ff">&gt;</span> */

//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(&quot;India&quot;);
c1.addItem(&quot;USA&quot;);
c1.addItemListener(this);
add(c1);

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

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



READ  Book Review - Murach's Beginning Java with NetBeans

Leave a Reply

Your email address will not be published.