here is the code

/*
	Chapter 6:	Borders
	Programmer: ELIIIIIIIIIIIIIIIIIIIIIIIIII DOOOOOOOOONNNNNNNNAAAAAAAAAAAAAAAAAAAAAAAHHHHHHUUUUUUUEEEEEEEEEE
	Date:
	Filename:	Borders.java
	Purpose:
*/

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


public class Borders2 extends Frame implements ActionListener, ItemListener
{
	public Borders2()
	{
		setBackground(Color.red);
		//set the layout
		setLayout(new BorderLayout(50,50));

      	//Add buttons
      	Button Red = new Button("Red");
      	Button Yellow = new Button("Yellow");
      	Button Cyan = new Button("Cyan");
      	Button Magenta = new Button("Magenta");

      	Choice choicepanel = new Choice();
				choicepanel.add(" ");
				choicepanel.add("Red");
				choicepanel.add("Yellow");
				choicepanel.add("Cyan");
				choicepanel.add("Magenta");
				choicepanel.add("White");
		add(choicepanel);


		Red.addActionListener(this);
		Yellow.addActionListener(this);
		Cyan.addActionListener(this);
		Magenta.addActionListener(this);
		choicepanel.addItemListener(this);

		Red.setActionCommand("Red");
		Yellow.setActionCommand("Yellow");
		Cyan.setActionCommand("Cyan");
		Magenta.setActionCommand("Magenta");


		add(Red, BorderLayout.NORTH);
		add(Yellow, BorderLayout.SOUTH);
		add(Cyan, BorderLayout.EAST);
		add(Magenta, BorderLayout.WEST);
		add(choicepanel, BorderLayout.CENTER);

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

	}

			public void actionPerformed(ActionEvent e)
		{
			String arg = e.getActionCommand();

			if(arg == "Red")
				setBackground(Color.red);

			if(arg == "Yellow")
				setBackground(Color.yellow);

			if(arg == "Cyan")
				setBackground(Color.cyan);

			if(arg == "Magenta")
				setBackground(Color.magenta);

			}

			public void itemStateChanged(ItemEvent ie)
			{
				String arg = e.getActionCommand();

				if(arg == "Red")
				setBackground(Color.red);

				if(arg == "Yellow")
				setBackground(Color.yellow);

				if(arg == "Cyan")
				setBackground(Color.cyan);

				if(arg == "Magenta")
				setBackground(Color.magenta);
			}




   	public static void main(String[] args)
   	{




	   	// set frame properties
		Borders f = new Borders();
      	f.setTitle("Border Application");
      	f.setBounds(200,200,300,300);
	    f.setVisible(true);
   }
}

the problem im having is in the itemstate changed fucntion. it says that it cant find the variable e in String arg = e.getActionCommand();. i cant find the answer in my book.

can anyone help?

Recommended Answers

All 4 Replies

>public void itemStateChanged(ItemEvent ie)
Either that's a typo and ie should be e, or you need to change any use of e to ie.

It appears you have copied the code from the actionPerformed() method directly into the itemStateChanged() method. The parameter to itemStateChanged function is "ie", not "e" as it was in actionPerformed.

Even if you change the "e" to "ie", the code will still not work since getActionCommand is a method of the ActionEvent, not ItemEvent. With ItemEvent, you will need to use getItem() to determine the item that was selected or deselected.

ok i went back and tried to fix it

this is what i came up with

(im kinda new to java)

public void itemStateChanged(ItemEvent ie)
   {
    String arg = choicepanel.getSelectedItem();
    if(arg == "red")
     setBackground(Color.red);

   }

i think im going about this the wrong way

Use Object arg = ie.getItem() to get the object that triggered the event.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.