title: What is difference between event.getItem() and comboBox.getSelectedItem() while casting the object
body: <p>I have a panel with a combobox. I am adding id numbers to the combobox. But before adding any id, I am adding a empty String to know that no id is selected.</p>
  
  <pre><code>JComboBox jcPatientIds;
  JPanel patientPanel;
  
  public void updateIdsInComboBoxes(ArrayList&lt;Integer&gt; items)
  {
      getJcPatientIds().addItem("");
      if (items.size() &gt; 0)
      {
          for (Integer item : items)
          {
              getJcPatientIds().addItem(item);
          }
      }
      getPatientPanel().add(getJcPatientIds());
  }
  
  public JComboBox getJcPatientIds()
  {
      if(jcPatientIds == null)
      {
      jcPatientIds = new JComboBox();
      jcPatientIds.setBounds(250, 20, 100, 20);
      }
      return jcPatientIds;
  }
  
  public JPanel getPatientPanel()
  {
      if(patientPanel == null)
      {
          patientPanel = new JPanel();
      }
      return patientPanel;
  }
  </code></pre>
  
  <p>Now, I have listener class as like this. And it works fine without giving any error</p>
  
  <pre><code>@Override
  public void itemStateChanged(ItemEvent e)
  {
      /*The event triggers for both deselected and selected item
      Here only the   selected event will be triggered with value 1
                      deselected event has a value 2*/
      if (e.getStateChange() == 1)
      {
          if (e.getItem().equals(""))
          {
              // Some stuff
          }
          else
          {
          System.out.println((int) e.getItem());
              // Some other stuff
          }
      }
  }
  </code></pre>
  
  <p>But, If I use getSelectedItem on Combobox. I am getting ClassCastException. What is the problem here</p>
  
  <pre><code>@Override
  public void itemStateChanged(ItemEvent e)
  {
          if (getJcPatientIds.getSelectedItem().equals(""))
          {
              // Some stuff
          }
          else
          {
          System.out.println((int) getJcPatientIds.getSelectedItem()); // Here I am getting ClassCastException
              // Some other stuff
          }
      }
  }
  </code></pre>
  
  <p>Error</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
  at hospitalApp.common.PatientController.itemStateChanged(PatientController.java:71)
  at javax.swing.JComboBox.fireItemStateChanged(JComboBox.java:1225)
  at javax.swing.JComboBox.selectedItemChanged(JComboBox.java:1273)
  at javax.swing.JComboBox.contentsChanged(JComboBox.java:1329)
  at javax.swing.AbstractListModel.fireContentsChanged(AbstractListModel.java:118)
  at javax.swing.DefaultComboBoxModel.setSelectedItem(DefaultComboBoxModel.java:93)
  at javax.swing.JComboBox.setSelectedItem(JComboBox.java:578)
  at javax.swing.JComboBox.setSelectedIndex(JComboBox.java:624)
  at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(BasicComboPopup.java:835)
  at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
  at java.awt.Component.processMouseEvent(Component.java:6516)
  at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
  at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(BasicComboPopup.java:499)
  at java.awt.Component.processEvent(Component.java:6281)
  at java.awt.Container.processEvent(Container.java:2229)
  at java.awt.Component.dispatchEventImpl(Component.java:4872)
  at java.awt.Container.dispatchEventImpl(Container.java:2287)
  at java.awt.Component.dispatchEvent(Component.java:4698)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
  at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
  at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
  at java.awt.Container.dispatchEventImpl(Container.java:2273)
  at java.awt.Window.dispatchEventImpl(Window.java:2719)
  at java.awt.Component.dispatchEvent(Component.java:4698)
  at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
  at java.awt.EventQueue.access$300(EventQueue.java:103)
  at java.awt.EventQueue$3.run(EventQueue.java:706)
  at java.awt.EventQueue$3.run(EventQueue.java:704)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
  at java.awt.EventQueue$4.run(EventQueue.java:720)
  at java.awt.EventQueue$4.run(EventQueue.java:718)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
  at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
  at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
  at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
  at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
  </code></pre>