Solved

itemStateChanged()

Posted on 2010-11-09
3
891 Views
Last Modified: 2013-11-23
I'm a current student, and my homework assignment is to edit a
form that uses a BorderLayout. I'm having problems with the
itemStateChanged() method. In the method, I declare a new
integer variable that uses the getStateChange() method. However,
when I run the applet, only color yellow shows, and I can't figure out
how to solve this. Any ideas?
package buttons;

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

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");
      	//Button white = new Button("White");
        Choice colors = new Choice();
        colors.add("Red");
        colors.add("Yellow");
        colors.add("Cyan");
        colors.add("Magenta");
        colors.add("White");

        red.addActionListener(this);
        yellow.addActionListener(this);
        cyan.addActionListener(this);
        magenta.addActionListener(this);
        //white.addActionListener(this);
        colors.addItemListener(this);

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



		//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("Border Application");
      	f.setBounds(200,200,300,300);
	    f.setVisible(true);
            f.setBackground(Color.RED);
   }

        public void actionPerformed(ActionEvent e)
    {
            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
            {
                setBackground(Color.WHITE);
            }
        }

        public void itemStateChanged(ItemEvent ie)
    {
            int arg = ie.getStateChange();

            if (arg == 0)
            {
                setBackground(Color.RED);
            }
                else if (arg == 1)
                {
                    setBackground(Color.YELLOW);
                }
                else if (arg == 2)
                {
                    setBackground(Color.CYAN);
                }
                else if (arg == 3)
                {
                    setBackground(Color.MAGENTA);
                }
            else
            {
                setBackground(Color.WHITE);
            }
        }
}

Open in new window

0
Comment
Question by:Keith1985
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 34099659
try this,

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * TYPE DESCRIPTION HERE
 *
 */
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");
            //Button white = new Button("White");
        Choice colors = new Choice();
        colors.add("Red");
        colors.add("Yellow");
        colors.add("Cyan");
        colors.add("Magenta");
        colors.add("White");

        red.addActionListener(this);
        yellow.addActionListener(this);
        cyan.addActionListener(this);
        magenta.addActionListener(this);
        //white.addActionListener(this);
        colors.addItemListener(this);

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



            //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("Border Application");
            f.setBounds(200,200,300,300);
          f.setVisible(true);
            f.setBackground(Color.RED);
   }

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

            performAction( arg ) ;
        }
       
        private void performAction( String command ){
            if (command.equals( "Red" ) )
            {
                setBackground(Color.RED);
            }
                else if (command.equals( "Yellow" ) )
                {
                    setBackground(Color.YELLOW);
                }
                else if ( command.equals( "Cyan" ) )
                {
                    setBackground(Color.CYAN);
                }
                else if ( command.equals( "Magenta" ) )
                {
                    setBackground(Color.MAGENTA);
                }
            else
            {
                setBackground(Color.WHITE);
            }
        }

        public void itemStateChanged(ItemEvent ie)
    {
            int arg = ie.getStateChange();
            performAction( ie.getItem().toString() ) ;
        }
}
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 34099662
its better to deal with lightweight swing compoenents instead of AWT!
0
 

Author Closing Comment

by:Keith1985
ID: 34108231
Thank you very much. Yeah I like swing components as well, and they are much simpler to use and reference.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question