Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,334 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,826 people online right now. Registration is fast and FREE... Join Now!




Cannot find symbol (how can I solve it?)

 
Reply to this topicStart new topic

Cannot find symbol (how can I solve it?), about method getSelectedIndex()

topaz13_mely
2 Sep, 2008 - 06:59 PM
Post #1

New D.I.C Head
*

Joined: 2 Sep, 2008
Posts: 2

CODE

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

public class Choice extends Applet implements ItemListener
{
       TextField txtNames = new TextField(15);
       Choice chNames = new Choice();

public void init()
{
        chNames.add("Naruto");
        chNames.add("Sasuke");
        chNames.add("Sakura");
        add(chNames);
        add(txtNames); chNames.addItemListener(this);
        }

public void itemStateChanged(ItemEvent y)
        {
               if(chNames.getSelectedIndex()==0)
                        txtNames.setText("Uzumaki");
                else if(chNames.getSelectedIndex()==1)
                       txtNames.setText("uchicha");
                else
                       txtNames.setText("Hanuro");
                        }
}                                        



This post has been edited by topaz13_mely: 2 Sep, 2008 - 07:04 PM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Cannot Find Symbol (how Can I Solve It?)
2 Sep, 2008 - 08:22 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,110



Thanked: 202 times
Dream Kudos: 75
My Contributions
chNames is an object of type Choice which extends JApplet
Neither Choice or JApplet have a method add(String);
neither have a method named addItemListener()

May be you were thinking of a ComboBox:

CODE

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

import javax.swing.*;

public class Choice extends Applet implements ItemListener
{
       TextField txtNames = new TextField(15);
       JComboBox chNames = new JComboBox();

public void init()
{
        chNames.addItem("Naruto");
        chNames.addItem("Sasuke");
        chNames.addItem("Sakura");
        add(chNames);
        add(txtNames);
        chNames.addItemListener(this);
        }

public void itemStateChanged(ItemEvent y) {
            txtNames.setText((String) chNames.getSelectedItem());
        }
}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:58AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month