Solved

itemStateChanged()

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delete Android all or certain applications data programmatically 9 141
Html split(text) 2 45
Java Inheritance super keyword use 8 68
Bitwise and to sum elements 2 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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.
Suggested Courses

738 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