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

  • SEARCH
  • Home
  • 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 9010043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:21:21+00:00 2026-06-16T02:21:21+00:00

I am trying to write a ListSelectionListener for a JList that knows which list

  • 0

I am trying to write a ListSelectionListener for a JList that knows which list item the user is selecting away from, and which list item the user is selecting to. So if a list has three items in it {Apple, Orange, Pear}, and the current selection is on Orange and the user selects Pear, then:

  • srcFruit is Orange; and
  • destFruit is Pear

Here’s the code I have:

myList.addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent fruitSelectionEvent) {
        printSourceAndDestFruit(myList, fruitSelectionEvent);
    }
});

private void printSourceAndDestFruit(JList list, ListSelectionEvent event) {
    FruitVO srcFruit = (FruitVO)list.getModel().getElementAt(event.getFirstIndex());
    FruitVO destFruit = (FruitVO)list.getModel().getElementAt(event.getLastIndex());

    System.out.println("srcFruit = " + srcFruit.getName() + " and destFruit = " = destFruit.getName());
}

When the application loads and initializes the JList, there is no default selection. When I take the following actions:

  1. Click Orange
  2. Click Pear
  3. Click Orange again

Here’s the print out I get:

srcFruit = Orange and destFruit = Pear
srcFruit = Orange and destFruit = Pear

Where am I going wrong here? Are getFirstIndex()/getLastIndex() buggy or just not the correct Swing methods to be using?

Here’s the output I should be seeing:

srcFruit = Orange and destFruit = Pear
srcFruit = Pear and destFruit = Orange

So even though I made 3 selects (mouse clicks), since the first time I click Orange wasn’t a change from one value to the next, I believe it is correct to not fire and call printSourceAndDestFruit. The I select Pear and it is correct in stating that srcFruit is Orange and that destFruit is Pear. But when I click back to Orange the 2nd println should have srcFruit as Pear and destFruit as Orange. Why doesn’t it?!?!

Thanks in advance!

  • 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-16T02:21:22+00:00Added an answer on June 16, 2026 at 2:21 am

    The first and last index are not what you think they are. Here’s what the javadoc says:

    getFirstIndex()

    Returns the index of the first row whose selection may have changed.

    getLastIndex()

    Returns the index of the last row whose selection may have changed.

    So, since the selection of pear and orange changes at each click, and since pear is after orange in the list, getFirstIndex() always returns Orange and getLastIndex() always returns Pear.

    If you want to compare the new selection with the last one, then keep the last selection in some member variable, and compare it with the new selection each time the selection changes, and getValueIsAdjusting() returns false.

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

Sidebar

Related Questions

I've had problem when trying write a function which has a default value when
I'm trying write a simple perl script that reads some fields from a password
Im trying to write out an image file that has the following structure: P5
Trying to write a python application that downloads images from an RSS feed, and
Trying to write function that numerize items of list and returns list of tuples
I'm trying write a program that takes a list of names, sorts those names,
Trying to write a procedure that given a value and a list, it deletes
I am trying write a query that will display if the person has anyone
I'm trying write a Ruby script that checks if user credentials are valid using
I'm trying write a program that takes a list of names, sorts those names,

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.