Guessing Game Exception Problem

I have a problem with my code... I can't tell why but the problem says:

addActionListener(java.awt.event.ActionListener) in javax.swing.JTextField cannot be applied to (RandomGuess2) --line 56

I'm quite a newbie in the world of Java so can anyone give a hand? Don't mind the pics yet... What's important is the one in JTextField.. >.>

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.text.DecimalFormat;

publicclass RandomGuess2extends JPanel{

JCheckBox dress;

JCheckBox hair;

JCheckBox other;

JCheckBox face;

JTextField inguess;

int num1;

StringBuffer choices;

JLabel guesspic;

CheckBoxListen myListener =null;

public RandomGuess2(){

//myListener for the checkbox

myListener =new CheckBoxListen();

hair =new JCheckBox("clue 1");

hair.addItemListener(myListener);

other =new JCheckBox("clue 2");

other.addItemListener(myListener);

face =new JCheckBox("clue 3");

face.addItemListener(myListener);

dress =new JCheckBox("clue 4");

dress.addItemListener(myListener);

// Indicates what's on the geek.

choices =new StringBuffer("dhfr");//Default Image has all the parts.

// Set up the picture label

guesspic =new JLabel(new ImageIcon("haru-" + choices.toString().trim() +".jpg"));

guesspic.setToolTipText(choices.toString().trim());

// Put the check boxes in a column in a panel

JPanel jplCheckBox =new JPanel();

jplCheckBox.setLayout(new GridLayout(0, 1));//0 rows, 1 Column

jplCheckBox.add(hair);

jplCheckBox.add(other);

jplCheckBox.add(face);

jplCheckBox.add(dress);

//input text field for the final guess

inguess =new JTextField(10);

inguess.addActionListener(this);

JLabel choi =new JLabel("CHOICES: 1. Suzumiya Haruhi2. Fujioka Haruhi");

setLayout(new BorderLayout());

add(inguess, BorderLayout.NORTH);

add(jplCheckBox, BorderLayout.WEST);

add(guesspic, BorderLayout.CENTER);

add(choi, BorderLayout.SOUTH);

setBorder(BorderFactory.createEmptyBorder(50,50,50,50));

}

//Listens to the check boxes events

class CheckBoxListenimplements ItemListener{

publicvoid itemStateChanged(ItemEvent e){

int index = 0;

char c ='-';

Object source = e.getSource();

if (source == dress){

index = 0;

c ='d';

}elseif (source == hair){

index = 1;

c ='h';

}elseif (source == face){

index = 2;

c ='f';

}elseif (source == other){

index = 3;

c ='r';

}

if (e.getStateChange() == ItemEvent.DESELECTED)

c ='-';

choices.setCharAt(index, c);

guesspic.setIcon(new ImageIcon("haru-" + choices.toString().trim() +".jpg"));

guesspic.setToolTipText(choices.toString());

}

}

publicvoid actionPerformed(ActionEvent evt)

{

String rest ="";

try{

num1 = Integer.parseInt(inguess.getText());

if(num1 == 1){rest ="CORRECT!!";}

else{rest ="WRONG!!";}

}

catch(NumberFormatException numFormExcept){

JOptionPane.showMessageDialog(this,"You must enter either 1 or 2","Invalid Format", JOptionPane.ERROR_MESSAGE);

}

}

publicstaticvoid main(String s[]){

JFrame fame =new JFrame("JCheckBox Usage Demo");

fame.addWindowListener(new WindowAdapter(){

publicvoid windowClosing(WindowEvent e){

System.exit(0);

}

});

fame.setContentPane(new RandomGuess2());

fame.pack();

fame.setVisible(true);

}

}

[7203 byte] By [hikariSolisa] at [2007-11-26 19:53:51]
# 1
inguess.addActionListener(this);Here you are trying to attach this (ie the RandomGuess2 class) as an ActionListener to the JTextField but RandomGuess2 is not an ActionListener. Maybe you need to implement it.
floundera at 2007-7-9 22:45:57 > top of Java-index,Java Essentials,New To Java...
# 2

> > inguess.addActionListener(this);

>

> Here you are trying to attach this (ie the

> RandomGuess2 class) as an ActionListener to the

> JTextField but RandomGuess2 is not an ActionListener.

> Maybe you need to implement it.

then... can I ask how to implement it? I only copied the code from a book so... I don't quite get it...

hikariSolisa at 2007-7-9 22:45:57 > top of Java-index,Java Essentials,New To Java...
# 3

Ok let's put it this way: In the API addActionListener(ActionListener a), requires an ActionListener as arguement, and now you're putting "this"(means the current class/instance) inside it. It will only work when "this" itself an ActionListener. You can convert your class to suit the need by "class xxx implements ActionListener" and overwrite methods inside interface ActionListner. I'm pretty sure the book you copied for done it that way, so referencing it back should help.

Icycoola at 2007-7-9 22:45:57 > top of Java-index,Java Essentials,New To Java...