Solved

itemStateChanged()

Posted on 2010-11-09
3
863 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now