Search code examples
javaswingeventsjcheckboxitemlistener

What is the difference between getItem(), getSelectableItem() and getSource()?


I don't really understand the difference between the following methods for an ItemEvent object, especially in the code example below:

Object getItem() from class ItemEvent, Java-API:

Returns the item affected by the event.

ItemSelectable getItemSelectable() from class ItemEvent, Java-API:

Returns the originator of the event.

Object getSource() inherited from class EventObject, Java-API:

Returns the object on which the Event initially occurred.

What I know is, that getItemSelectable() simplifies getting the object, because I do not have to cast explicitly to use methods like getText(). (So the (JCheckBox) cast in the second println command is not necessary.) And I know, that getItemSelectable() uses getSource(). But why is there another getItem()?

But the example below does not show any difference between those methods:

JCheckBox cb = new JCheckBox("text of checkbox", true);

ItemListener myListener = new ItemListener()
    {
        @Override
        public void itemStateChanged(ItemEvent e)
        {
            System.out.println(((JCheckBox) e.getItem()).getText());
            System.out.println(((JCheckBox) e.getSource()).getText());
            System.out.println(((JCheckBox) e.getItemSelectable()).getText());
        }
    };

cb.addItemListener(myListener);

Output:

text of checkbox

text of checkbox

text of checkbox

So what is the exact difference and when do I use which function?

Edit: Maybe there is no difference, at least no conceptual difference (except of return type and original class)?


Solution

  • If you take a look at the definition of the ItemEvent constructor :

     /**
     * Constructs an <code>ItemEvent</code> object.
     * <p> This method throws an
     * <code>IllegalArgumentException</code> if <code>source</code>
     * is <code>null</code>.
     *
     * @param source The <code>ItemSelectable</code> object
     *               that originated the event
     * @param id           The integer that identifies the event type.
     *                     For information on allowable values, see
     *                     the class description for {@link ItemEvent}
     * @param item   An object -- the item affected by the event
     * @param stateChange  An integer that indicates whether the item was
     *               selected or deselected.
     *                     For information on allowable values, see
     *                     the class description for {@link ItemEvent}
     * @throws IllegalArgumentException if <code>source</code> is null
     * @see #getItemSelectable()
     * @see #getID()
     * @see #getStateChange()
     */
    public ItemEvent(ItemSelectable source, int id, Object item, int stateChange) {
        super(source, id);
        this.item = item;
        this.stateChange = stateChange;
    }
    

    The ItemSelectable source parameter is the element that will be returned by e.getSource(). The Object item parameter is the element that will be returned by e.getItem().

    So actually the question is when the constructor is called with different object for source and item?

    Looking at the JCheckBox super classes - JToggleButton and AbstractButton, the ItemEvent is always constructed with the same object for both parameter. So maybe in some custom implementation the use of these differents methods make sense.