Problem with code

Hi I need some help with the following program, i followed what the book told me to do but i keep getting an error when it comes to my Item State Changed section, any help would be appreciated. Prior to putting this section in my program worked.

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

public class Buttons extends Frame
implements ActionListener, ItemListener{
public void actionPerformed(ActionEvent e)
{
//creating the action for pressing the buttons
String arg= e.getActionCommand();
if (arg=="Red") setBackground(Color.red);
else if (arg=="Yellow")
setBackground(Color.yellow);
else if (arg=="Cyan")
setBackground(Color.cyan);
else if (arg=="Magenta")
setBackground(Color.magenta);
else if (arg=="White") setBackground(Color.white);
}

//ItemChanged option
public void itemStateChanged(ItemEvent ie)
{
String color= ie.itemStateChanged();
if (color=="red")
setBackground(Color.red);
else if (color=="yellow")
setBackground(Color.yellow);
else if (color=="cyan")
setBackground(Color.cyan);
else if (color=="magenta")
setBackground(Color.magenta);
else if (color=="white")
setBackground(Color.white);
}

public Buttons()
{
//set the layout
setLayout(new BorderLayout(20,5));
setBackground(Color.red);

//Add buttons
Button red = new Button("Red");
Button yellow = new Button("Yellow");
Button cyan = new Button("Cyan");
Button magenta = new Button("Magenta");
Choice color =new Choice();

add(red, BorderLayout.NORTH);
add(yellow, BorderLayout.SOUTH);
add(cyan, BorderLayout.EAST);
add(magenta, BorderLayout.WEST);
add(color, BorderLayout.CENTER);

//adding the ActionListener() method to the buttons
red.addActionListener(this);
yellow.addActionListener(this);
cyan.addActionListener(this);
magenta.addActionListener(this);


//inserts the color choices into the Choice() construct
color.addItemListener(this);
color.insert("red", 1);
color.insert("yellow",2);
color.insert("cyan",3);
color.insert("magenta",4);
color.insert("white",5);


//override the windowClosing event
addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);

}

public static void main(String[] args)
{
// set frame properties
Buttons f = new Buttons();
f.setTitle("Button Application");
f.setBounds(200,200,300,300);
f.setVisible(true);
}
}

Comments

  • : Hi I need some help with the following program, i followed what the book told me to do but i keep getting an error when it comes to my Item State Changed section, any help would be appreciated. Prior to putting this section in my program worked.
    :
    : import java.awt.*;
    : import java.awt.event.*;
    :
    : public class Buttons extends Frame
    : implements ActionListener, ItemListener{
    : public void actionPerformed(ActionEvent e)
    : {
    : //creating the action for pressing the buttons
    : String arg= e.getActionCommand();
    : if (arg=="Red") setBackground(Color.red);
    : else if (arg=="Yellow")
    : setBackground(Color.yellow);
    : else if (arg=="Cyan")
    : setBackground(Color.cyan);
    : else if (arg=="Magenta")
    : setBackground(Color.magenta);
    : else if (arg=="White") setBackground(Color.white);
    : }
    :
    : //ItemChanged option
    : public void itemStateChanged(ItemEvent ie)
    : {
    : String color= ie.itemStateChanged();
    : if (color=="red")
    : setBackground(Color.red);
    : else if (color=="yellow")
    : setBackground(Color.yellow);
    : else if (color=="cyan")
    : setBackground(Color.cyan);
    : else if (color=="magenta")
    : setBackground(Color.magenta);
    : else if (color=="white")
    : setBackground(Color.white);
    : }
    :
    : public Buttons()
    : {
    : //set the layout
    : setLayout(new BorderLayout(20,5));
    : setBackground(Color.red);
    :
    : //Add buttons
    : Button red = new Button("Red");
    : Button yellow = new Button("Yellow");
    : Button cyan = new Button("Cyan");
    : Button magenta = new Button("Magenta");
    : Choice color =new Choice();
    :
    : add(red, BorderLayout.NORTH);
    : add(yellow, BorderLayout.SOUTH);
    : add(cyan, BorderLayout.EAST);
    : add(magenta, BorderLayout.WEST);
    : add(color, BorderLayout.CENTER);
    :
    : //adding the ActionListener() method to the buttons
    : red.addActionListener(this);
    : yellow.addActionListener(this);
    : cyan.addActionListener(this);
    : magenta.addActionListener(this);
    :
    :
    : //inserts the color choices into the Choice() construct
    : color.addItemListener(this);
    : color.insert("red", 1);
    : color.insert("yellow",2);
    : color.insert("cyan",3);
    : color.insert("magenta",4);
    : color.insert("white",5);
    :
    :
    : //override the windowClosing event
    : addWindowListener(
    : new WindowAdapter()
    : {
    : public void windowClosing(WindowEvent e)
    : {
    : System.exit(0);
    : }
    : }
    : );
    :
    : }
    :
    : public static void main(String[] args)
    : {
    : // set frame properties
    : Buttons f = new Buttons();
    : f.setTitle("Button Application");
    : f.setBounds(200,200,300,300);
    : f.setVisible(true);
    : }
    : }
    :
    What error do you get? Compile error, runtime error, syntax error, unknown identifier, type-mismatch, etc., etc.?
  • Sorry about that I knew I forgot something ;) here is the error I get:

    C:cs200Chapter 6Buttons.java:33: cannot find symbol
    symbol : method itemStateChanged()
    location: class java.awt.event.ItemEvent
    String color= ie.itemStateChanged();
    ^

    Thanks for any help you can provide

    Matt
  • : Sorry about that I knew I forgot something ;) here is the error I get:
    :
    : C:cs200Chapter 6Buttons.java:33: cannot find symbol
    : symbol : method itemStateChanged()
    : location: class java.awt.event.ItemEvent
    : String color= ie.itemStateChanged();
    : ^
    :
    : Thanks for any help you can provide
    :
    : Matt
    :
    itemStateChanged() is not a method of the ItemEvent. See this site: for all the methods of that class:
    http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/ItemEvent.html
    And then change that method call to the most appropriate one.
  • Is there anyway you can help me with that section of code? I tried several different ways with what you told me last time and still cannot get it to work. Thanks in advance.

    Matt
  • : Is there anyway you can help me with that section of code? I tried several different ways with what you told me last time and still cannot get it to work. Thanks in advance.
    :
    : Matt
    :
    Depending on what you want to know from the event, you should use either:
    - getItem()
    - getItemSelectable()
    - getStateChange()
    - paramString()
    By type-casting the getItemSelectable() result, you can also access the methods of the object, which generated the event.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories