title: Getters and Setters Through EventObject - ClassCastException Same Class (Java)
body: <p>I'm trying to get and set values of a class through an EventObject, but when casting the <code>getSource()</code> to the proper class, I run into a ClassCastException with the same class. How can I get around this while being able to get and set values through the EventObject?</p>
  
  <p>Thank you,
  Hunter</p>
  
  <p>ItemPanelEvent:</p>
  
  <pre><code>import java.util.EventObject;
  
  public class ItemPanelEvent extends EventObject implements Items{
  
  ItemPanel itemPanel;
  
  public ItemPanelEvent(Object source) {
      super(source);
  }
  
  public ItemPanelEvent(Object source, int position) {
      super(source);
      itemPanel = (ItemPanel) getSource();
      itemPanel.setPosition(position);
  }
  
  public int getPosition() {
      itemPanel = (ItemPanel) getSource();
      return itemPanel.getPosition();
  }
  
  public void setPosition(int position) {
      itemPanel = (ItemPanel) getSource();
      itemPanel.setPosition(position);
  }
  
  }
  </code></pre>
  
  <p>ItemPanel:</p>
  
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.GridBagConstraints;
  import java.awt.GridBagLayout;
  import java.awt.Insets;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.AbstractAction;
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  
  public class ItemPanel extends JPanel implements Items{
  
  private int position;
  
  private String item;
  private String buyPrice;
  private String sellPrice;
  private String quantity;
  private String pcBuyPrice;
  private String pcSellPrice;
  
  private JLabel itemLabel;
  private JLabel buyPriceLabel;
  private JLabel sellPriceLabel;
  private JLabel quantityLabel;
  private JLabel pcBuyPriceLabel;
  private JLabel pcSellPriceLabel;
  
  private JLabel itemValue;
  private JLabel buyPriceValue;
  private JLabel sellPriceValue;
  private JLabel quantityValue;
  private JLabel pcBuyPriceValue;
  private JLabel pcSellPriceValue;
  
  private JButton logBtn;
  private JButton editBtn;
  private JButton cancelBtn;
  
  
  
  private ItemPanelListener itemPanelListener;
  
  public ItemPanel(int position, String item, String buyPrice, String sellPrice,
          String quantity, String pcBuyPrice, String pcSellPrice) {
      this.position = position;
      this.item = item;
      this.buyPrice = buyPrice;
      this.sellPrice = sellPrice;
      this.quantity = quantity;
      this.pcBuyPrice = pcBuyPrice;
      this.pcSellPrice = pcSellPrice;
  
      Dimension dim = getPreferredSize();
      dim.height = 100;
      setPreferredSize(dim);
  
      setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
  
      logBtn = new JButton("Log Item");
      editBtn = new JButton("Edit Item");
      cancelBtn = new JButton("Cancel Item");
  
  
      setupLabels();
      setupCancelItemButton();
  
      layoutComponents();
  
  
  }
  
  public int getPosition() {
      return position;
  }
  
  public void setPosition(int pos) {
      position = pos;
  }
  
  public void setupCancelItemButton() {
      cancelBtn.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
  
              ItemPanelEvent ev = new ItemPanelEvent(this);
  
              if (itemPanelListener != null) {
                  itemPanelListener.itemPanelEventOccurred(ev);
              }
  
          }
      });
  }
  
  public void setItemPanelListener(ItemPanelListener listener) {
      this.itemPanelListener = listener;
  }
  
  public void setupLabels() {
      itemLabel = new JLabel("ITEM:");
      buyPriceLabel = new JLabel("BUY PRICE:");
      sellPriceLabel = new JLabel("SELL PRICE:");
      quantityLabel = new JLabel("QUANTITY:");
      pcBuyPriceLabel = new JLabel("PC BUY PRICE:");
      pcSellPriceLabel = new JLabel("PC SELL PRICE:");
  
      itemValue = new JLabel(this.item);
      buyPriceValue = new JLabel(this.buyPrice);
      sellPriceValue = new JLabel(this.sellPrice);
      quantityValue = new JLabel(this.quantity);
      pcBuyPriceValue = new JLabel(this.pcBuyPrice);
      pcSellPriceValue = new JLabel(this.pcSellPrice);
  }
  
  public void layoutComponents() {
      GridBagConstraints gc = new GridBagConstraints();
      setLayout(new GridBagLayout());
      gc.fill = GridBagConstraints.NONE;
  
      // First Row
      gc.gridy = 0;
      gc.weighty = 1;
      gc.gridwidth = 1;
  
      gc.gridx = 0;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(itemLabel, gc);
  
      gc.gridx = 1;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(itemValue, gc);
  
      gc.gridx = 2;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(buyPriceLabel, gc);
  
      gc.gridx = 3;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(buyPriceValue, gc);
  
      gc.gridx = 4;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(sellPriceLabel, gc);
  
      gc.gridx = 5;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(sellPriceValue, gc);
  
      gc.gridx = 6;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(quantityLabel, gc);
  
      gc.gridx = 7;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(quantityValue, gc);
  
      gc.gridx = 8;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(pcBuyPriceLabel, gc);
  
      gc.gridx = 9;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(pcBuyPriceValue, gc);
  
      gc.gridx = 10;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(pcSellPriceLabel, gc);
  
      gc.gridx = 11;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(pcSellPriceValue, gc);
  
      // Second Row
      gc.gridy = 1;
      gc.weighty = 1;
      gc.gridwidth = 1;
  
      gc.gridx = 6;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(logBtn, gc);
  
      gc.gridx = 8;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(editBtn, gc);
  
      gc.gridx = 10;
      gc.weightx = 1;
      gc.anchor = GridBagConstraints.CENTER;
      gc.insets = new Insets(0, 0, 0, 0);
      add(cancelBtn, gc);
  }
  
  }
  </code></pre>