Solved

itemStateChanged()

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 54
map related example 6 49
servlet filter example 37 63
hibernate example for saving data 19 43
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

803 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