简体   繁体   中英

about ItemEvent in java GUI

I'm doing a small program with Java GUI here is mu code:

    // DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{ 
   FlowLayout flow = new FlowLayout();
   JComboBox pizzaBox = new JComboBox();
   JLabel toppingList = new JLabel("Topping List");
   JLabel aLabel = new JLabel("Paulos's American Pie");
   JTextField totPrice = new JTextField(10);
   int[] pizzaPrice = {7,10,10,8,8,8,8};
   int totalPrice = 0;
   String output;
   int pizzaNum;
   public DebugFourteen3()
   {
      super("Pizza List");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      add(toppingList);
      pizzaBox.addItem("cheese");
      pizzaBox.addItem("sausage");
      pizzaBox.addItem("pepperoni");
      pizzaBox.addItem("onion");
      pizzaBox.addItem("green pepper");
      pizzaBox.addItem("green olive");
      pizzaBox.addItem("black olive");
      add(pizzaBox);
      add(aLabel);
      add(totPrice);
      itemStateChanged(this);
   }

   public static void main(String[] arguments)
   {
      JFrame frame = new DebugFourteen3();
      frame.setSize(200, 150);
      frame.setVisible(true);
   }

   public void itemStateChanged(ItemEvent[] list)
   {
      Object source = list.getSource();
      if(source == pizzaBox)
      {
         int pizzaNum = pizzaBox.getSelectedIndex();
         totalPrice = pizzaPrice[pizzaNum];
         output = "Pizza Price $" + totalPrice;
         totPrice.setText(output);
      }
   }
}

the compiler gets me error on line 35, it says itemStateChanged() should receive a argument which type is ItemEvent[] but I'm passing "this"(the class it self)

can anyone explain how the itemStateChanged works with the JComboBox? thanks

You have to add the listener on the combo box instead of calling the itemStateChanged method directly. Adding the listener will enable the callbacks when the click event happens, and jvm will call the itemStateChanged method automatically as combo box is registered for the event. Replace this line

  itemStateChanged(this);

with

pizzaBox.addItemListener(this);

At first, you need to implements ItemListener with JFrame . Than add following line with pizzaBox , this will notify overrided public void itemStateChanged(ItemEvent ev) method ItemListener .

pizzaBox.addItemListener(this);

For details, see this tutorial .

Problem :

1.you are not registering itemListener for JComboBox
2. itemStateChanged event takes ItemEvent as argument not ItemEvent[] array

1.Replace this :

itemStateChanged(this);

with this:

pizzaBox.addItemListener(this);

2.Replace This:

public void itemStateChanged(ItemEvent[] list)

With this:

public void itemStateChanged(ItemEvent list)

Complete Code:

// DebugFourteen3
// User selects pizza topping and sees price
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//use correct spelling of class name
public class DebugFourteen3 extends JFrame
{ 
   FlowLayout flow = new FlowLayout();
   JComboBox pizzaBox = new JComboBox();
   JLabel toppingList = new JLabel("Topping List");
   JLabel aLabel = new JLabel("Paulos's American Pie");
   JTextField totPrice = new JTextField(10);
   int[] pizzaPrice = {7,10,10,8,8,8,8};
   int totalPrice = 0;
   String output;
   int pizzaNum;
   public DebugFourteen3()
   {
      super("Pizza List");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setLayout(flow);
      add(toppingList);
      pizzaBox.addItem("cheese");
      pizzaBox.addItem("sausage");
      pizzaBox.addItem("pepperoni");
      pizzaBox.addItem("onion");
      pizzaBox.addItem("green pepper");
      pizzaBox.addItem("green olive");
      pizzaBox.addItem("black olive");
      add(pizzaBox);
      add(aLabel);
      add(totPrice);
      pizzaBox.addItemListener(this);
   }

   public static void main(String[] arguments)
   {
      JFrame frame = new DebugFourteen3();
      frame.setSize(200, 150);
      frame.setVisible(true);
   }

   public void itemStateChanged(ItemEvent list)
   {
      Object source = list.getSource();
      if(source == pizzaBox)
      {
         int pizzaNum = pizzaBox.getSelectedIndex();
         totalPrice = pizzaPrice[pizzaNum];
         output = "Pizza Price $" + totalPrice;
         totPrice.setText(output);
      }
   }
}

Here this ie DebugFourteen3 is not a type ItemEvent . Thats why

itemStateChanged(ItemEvent[] list)

gives error.

If you want to handle itemStateChanged then you need to implement ItemLisntener and add actionListner to it as

pizzaBox.addItemListener(this);

also add Handler method of ItemLisntener as

public void itemStateChanged(....)
{
    //Handling Code  goes here
}

Or you can implement ActionListner also For more information on how to use combobox you can visit here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM