?
Solved

itemStateChanged()

Posted on 2010-11-09
3
Medium Priority
?
900 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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