Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8188139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:48:25+00:00 2026-06-07T02:48:25+00:00

I don’t really understand the difference between the following methods for an ItemEvent object,

  • 0

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)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:48:27+00:00Added an answer on June 7, 2026 at 2:48 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't let below code scare you away . The question is really simple, only
Don't ask me how--I haven't the slightest. The following code crashes my terminal and
Don't be scared of the extensive code. The problem is general. I just provided
Don't really know how to formulate the title, but it should be pretty obvious
Don't ask me why but I need to do the following: string ClassName =
I don't know why, but this code worked for me a month ago... maybe
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't they both have to convert to machine code at some point to execute
Don't ask me how but I managed to get accidentally the following remote branches

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.