Search code examples
javaswingjcomboboxitemlistener

Item Listeners Error


I'm having an issue with Item Listeners.It's the first time I'm using it, so far all I've used is the Item Event. I was wondering if you could clear up what the difference between those two, as well point me out what I'm doing wrong.

My issue is on line 46 the line starting with: Object source = toppingList.getSource(); and the error I get is 'Cannot find symbol'.

I'm thinking I'm using the wrong item before the getSource();, I thought that the toppingList was the correct item, I can't see which other item I could put in it's place.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


public class Pizza 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 Pizza()
  {
    super("Pizza List");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(flow);
    pizzaBox.addItemListener((ItemListener) this);
    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);
  }

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


 public void itemStateChanged(ItemEvent[] list)
 {

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

Solution

  • Gui elements do not have any getSource, it is a method of the event - telling you which gui element generated the event. But you know what the source of the event is, since in your constructor you wrote:

    pizzaBox.addItemListener((ItemListener) this);
    

    and you did not add this to any other gui element. So you cannot get events from any other gui element. So do not test for it.

    But there are other issues:

    Your PizzaBox should implement ItemListener:

    public class Pizza extends JFrame implement ItemListener 
    

    and then just write

    pizzaBox.addItemListener(this);
    

    If you want to listen to multiple elements, add separate anonymous listener for each (and Pizza does not implement ItemListener)

    // in your constructor:
    pizzaBox.addItemListener(new ItemListener() { 
        public void itemStateChanged(ItemEvent e) {
           if (e.getStateChange() == ItemEvent.SELECTED) {
               pizzaNum = pizzaBox.getSelectedIndex(); // in your code you have int pizzaNum but at the same time, pizzaNum is a class variable, probably an error
               // and so on
           } 
        }
    });
    

    or you can move the code to a separate method

    public class Pizza extends JFrame {
    
        public Pizza() {
            :
            pizzaBox.addItemListener(new ItemListener() { 
                public void itemStateChanged(ItemEvent e) {
                     pizzaBox_itemStateChanged(e);
                }
            });
            :
        }
    
        private void pizzaBox_itemStateChanged(ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
               pizzaNum = pizzaBox.getSelectedIndex();
               // and so on
            }
        }
    
        :
    }