6 Replies - 12217 Views - Last Post: 12 January 2008 - 07:16 PM Rate Topic: -----

#1 dbrine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 25-April 07

Choice Component and AddItemListener

Posted 29 April 2007 - 07:02 PM

I have been working this code for hours. Yes I newbie here. i have everthing right until they said to

1. add ItemLister to the Buttons class header
2. Add itemListener to choice component and then ass choice component to center (did this)
3.write ItemStateChange to test each item component using public void itemStateChanged(ItemEvent ie) Use the same code as ActionPeformed()


I have looked and looked and haven't been able to figure it out. In the choice component then you select a color the background changes. Like when I codes the buttons, the background color changed.


Any help or advice would be appreciated
import java.awt.*;
import java.awt.event.*;

public class Buttons extends Frame implements ActionListener,ItemListener
{
	public Buttons()
	{
		//set the layout
		setLayout(new BorderLayout(20,5));

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

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

		//choice component colors
		colors.add("Red");
		colors.add("Yellow");
		colors.add("Cyan");
		colors.add("Magenta");

		//ItemLister for choice component
		Red.addItemListener(this);
		Yellow.addItemListener(this);
		Cyan.addItemListener(this);
		Magenta.addItemListener(this);
		colors.addItemListener(this);

		//add the ActionListener to each menu item
		Red.addActionListener(this);
		Yellow.addActionListener(this);
		Cyan.addActionListener(this);
		Magenta.addActionListener(this);
		//White.addActionListener(this);

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

	}

   	//This method is triggered by the user clicking in the choice component area in the center and choicing a color
   	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);

		   		if (arg == "White")
   			setBackground(Color.white);
	}

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

   }

   public void actionPerformed(ActionEvent e)
   	{
   		//test for button clicks
   		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);

   		if (arg == "White")
   			setBackground(Color.white);
	}

}
***edit: code tags

This post has been edited by PennyBoki: 15 September 2007 - 11:19 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Choice Component and AddItemListener

#2 dbrine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 25-April 07

Re: Choice Component and AddItemListener

Posted 02 May 2007 - 08:45 PM

no one??

View Postdbrine, on 29 Apr, 2007 - 07:02 PM, said:

I have been working this code for hours. Yes I newbie here. i have everthing right until they said to

1. add ItemLister to the Buttons class header
2. Add itemListener to choice component and then ass choice component to center (did this)
3.write ItemStateChange to test each item component using public void itemStateChanged(ItemEvent ie) Use the same code as ActionPeformed()


I have looked and looked and haven't been able to figure it out. In the choice component then you select a color the background changes. Like when I codes the buttons, the background color changed.


Any help or advice would be appreciated

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

public class Buttons extends Frame implements ActionListener,ItemListener
{
public Buttons()
{
//set the layout
setLayout(new BorderLayout(20,5));

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

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

//choice component colors
colors.add("Red");
colors.add("Yellow");
colors.add("Cyan");
colors.add("Magenta");

//ItemLister for choice component
Red.addItemListener(this);
Yellow.addItemListener(this);
Cyan.addItemListener(this);
Magenta.addItemListener(this);
colors.addItemListener(this);

//add the ActionListener to each menu item
Red.addActionListener(this);
Yellow.addActionListener(this);
Cyan.addActionListener(this);
Magenta.addActionListener(this);
//White.addActionListener(this);

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

}

//This method is triggered by the user clicking in the choice component area in the center and choicing a color
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);

if (arg == "White")
setBackground(Color.white);
}

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

}

public void actionPerformed(ActionEvent e)
{
//test for button clicks
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);

if (arg == "White")
setBackground(Color.white);
}

}

Was This Post Helpful? 0
  • +
  • -

#3 dbrine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 25-April 07

Re: Choice Component and AddItemListener

Posted 16 May 2007 - 03:34 PM

I finally figured it out. WOW!!!


View Postdbrine, on 29 Apr, 2007 - 07:02 PM, said:

I have been working this code for hours. Yes I newbie here. i have everthing right until they said to

1. add ItemLister to the Buttons class header
2. Add itemListener to choice component and then ass choice component to center (did this)
3.write ItemStateChange to test each item component using public void itemStateChanged(ItemEvent ie) Use the same code as ActionPeformed()


I have looked and looked and haven't been able to figure it out. In the choice component then you select a color the background changes. Like when I codes the buttons, the background color changed.


Any help or advice would be appreciated

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

public class Buttons extends Frame implements ActionListener,ItemListener
{
public Buttons()
{
//set the layout
setLayout(new BorderLayout(20,5));

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

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

//choice component colors
colors.add("Red");
colors.add("Yellow");
colors.add("Cyan");
colors.add("Magenta");

//ItemLister for choice component
Red.addItemListener(this);
Yellow.addItemListener(this);
Cyan.addItemListener(this);
Magenta.addItemListener(this);
colors.addItemListener(this);

//add the ActionListener to each menu item
Red.addActionListener(this);
Yellow.addActionListener(this);
Cyan.addActionListener(this);
Magenta.addActionListener(this);
//White.addActionListener(this);

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

}

//This method is triggered by the user clicking in the choice component area in the center and choicing a color
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);

if (arg == "White")
setBackground(Color.white);
}

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

}

public void actionPerformed(ActionEvent e)
{
//test for button clicks
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);

if (arg == "White")
setBackground(Color.white);
}

}

Was This Post Helpful? 0
  • +
  • -

#4 moses316  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 13-September 07

Re: Choice Component and AddItemListener

Posted 14 September 2007 - 05:49 PM

What did your final code end up looking like? I am having the same problem. Any help would be appreciated.

View Postdbrine, on 16 May, 2007 - 05:34 PM, said:

I finally figured it out. WOW!!!


View Postdbrine, on 29 Apr, 2007 - 07:02 PM, said:

I have been working this code for hours. Yes I newbie here. i have everthing right until they said to

1. add ItemLister to the Buttons class header
2. Add itemListener to choice component and then ass choice component to center (did this)
3.write ItemStateChange to test each item component using public void itemStateChanged(ItemEvent ie) Use the same code as ActionPeformed()


I have looked and looked and haven't been able to figure it out. In the choice component then you select a color the background changes. Like when I codes the buttons, the background color changed.


Any help or advice would be appreciated

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

public class Buttons extends Frame implements ActionListener,ItemListener
{
public Buttons()
{
//set the layout
setLayout(new BorderLayout(20,5));

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

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

//choice component colors
colors.add("Red");
colors.add("Yellow");
colors.add("Cyan");
colors.add("Magenta");

//ItemLister for choice component
Red.addItemListener(this);
Yellow.addItemListener(this);
Cyan.addItemListener(this);
Magenta.addItemListener(this);
colors.addItemListener(this);

//add the ActionListener to each menu item
Red.addActionListener(this);
Yellow.addActionListener(this);
Cyan.addActionListener(this);
Magenta.addActionListener(this);
//White.addActionListener(this);

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

}

//This method is triggered by the user clicking in the choice component area in the center and choicing a color
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);

if (arg == "White")
setBackground(Color.white);
}

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

}

public void actionPerformed(ActionEvent e)
{
//test for button clicks
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);

if (arg == "White")
setBackground(Color.white);
}

}

Was This Post Helpful? 0
  • +
  • -

#5 saltnmw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 08

Re: Choice Component and AddItemListener

Posted 12 January 2008 - 04:31 PM

Yes, please do tell - asap!
Was This Post Helpful? 0
  • +
  • -

#6 saltnmw  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 08

Re: Choice Component and AddItemListener

Posted 12 January 2008 - 06:22 PM

Here it is -
problem was variable colors had no public access; itemStateChanged method required not getActionCommand() but getSelectedItem(); and associated variable is not "e" as in actionPerformed, but "ie"; also multiple itemListeners are not necessary for colors.



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


public class Buttons2 extends Frame implements ActionListener, ItemListener
{
	Choice colors = new Choice();

	public Buttons2()
	{
		//set the layout
		setLayout(new BorderLayout(20,5));

		  //Add buttons
		  Button red = new Button("Red");
			  red.addActionListener(this);
		  Button yellow = new Button("Yellow");
			  yellow.addActionListener(this);
		  Button cyan = new Button("Cyan");
			  cyan.addActionListener(this);
		  Button magenta = new Button("Magenta");
			  magenta.addActionListener(this);



		add(red, BorderLayout.NORTH);
		add(yellow, BorderLayout.SOUTH);
		add(cyan, BorderLayout.EAST);
		add(magenta, BorderLayout.WEST);
		add(colors, BorderLayout.CENTER);
			colors.add("Colors");
			colors.add("Red");
			colors.add("Yellow");
			colors.add("Cyan");
			colors.add("Magenta");
			colors.add("White");

			colors.addItemListener(this);

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

	}//end Buttons2

   	public static void main(String[] args)
   	{
	   	// set frame properties
		Buttons2 f = new Buttons2();
		f.setBackground(Color.red);
		  f.setTitle("Border Application");
		  f.setBounds(200,200,300,300);
		f.setVisible(true);
	}//end main

   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);
	


   }//end actionPerformed

	public void itemStateChanged(ItemEvent ie)
   {
	   String arg = colors.getSelectedItem();

	   if (arg== "Red")
	   		setBackground(Color.red);
	   if (arg== "Yellow")
	   		setBackground(Color.yellow);
	   if (arg== "Cyan")
	   		setBackground(Color.cyan);
	   if (arg== "Magenta")
	   		setBackground(Color.magenta);
	   if (arg== "White")
	   		setBackground(Color.white);

   }//end itemStateChanged

}//end public class


This post has been edited by saltnmw: 12 January 2008 - 06:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5642
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Choice Component and AddItemListener

Posted 12 January 2008 - 07:16 PM

Well, since the answer is out... Here's my take.

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

public class Buttons2b extends Frame implements ActionListener, ItemListener {
	public Buttons2b() {
		//set the layout
		setLayout(new BorderLayout(20,5));

		add(getButton("Red"), BorderLayout.NORTH);
		add(getButton("Yellow"), BorderLayout.SOUTH);
		add(getButton("Cyan"), BorderLayout.EAST);
		add(getButton("Magenta"), BorderLayout.WEST);

		Choice colors = new Choice();
		colors.add("Colors");
		colors.add("Red");
		colors.add("Yellow");
		colors.add("Cyan");
		colors.add("Magenta");
		colors.add("White");
		colors.addItemListener(this);
		add(colors, BorderLayout.CENTER);
		//colors.select(str)

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

	private Button getButton(String name) {
		Button button = new Button(name);
		button.addActionListener(this);
		return button;
	}
	
	private void setChosenColor(String arg) {
	   if (arg.equals("Red")) {
			setBackground(Color.red);
		} else if (arg.equals("Yellow")) {
			setBackground(Color.yellow);
		} else if (arg.equals("Cyan")) {
			setBackground(Color.cyan);
		} else if (arg.equals("Magenta")) {
			setBackground(Color.magenta);
		} else if (arg.equals("White")) {
			setBackground(Color.white);
		}
	}

	public void actionPerformed(ActionEvent e) {
		setChosenColor(e.getActionCommand());
	}

	public void itemStateChanged(ItemEvent ie) {
		setChosenColor(((Choice)ie.getItem()).getSelectedItem());
	}

	public static void Test() {
		 // set frame properties
		Buttons2 f = new Buttons2();
		f.setBackground(Color.red);
		f.setTitle("Border Application");
		f.setBounds(200,200,300,300);
		f.setVisible(true);
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1