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 7623751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:46:58+00:00 2026-05-31T04:46:58+00:00

I’m trying to write my own implementation of ListSelectionModel and currently I’m stuck while

  • 0

I’m trying to write my own implementation of ListSelectionModel and currently I’m stuck while trying to implement insertIndexInterval. I don’t understand the result of this method in Sun’s/Oracle’s DefautListSelectionModel implementation. Here is an example:

ListSelectionModel model = new DefaultListSelectionModel();
model.setSelectionInterval(3, 5);
model.addListSelectionListener(new ListSelectionListener()
{
    public void valueChanged(ListSelectionEvent e)
    {
        System.out.println("Changed range reported by event: " +
            e.getFirstIndex() + "-" + e.getLastIndex());
    }
});

System.out.print("Selected indices before insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
    if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();

model.insertIndexInterval(3, 3, true);  

System.out.print("Selected indices after insert: ");
for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++)
    if (model.isSelectedIndex(i)) System.out.print(i + " ");
System.out.println();

When you run this code you will get this output:

Selected indices before insert: 3 4 5 
Changed range reported by event: 3-8
Selected indices after insert: 3 4 5 6 7 8 

So the initial selection was 3-5 and it was expanded to 3-8 when inserting new indices. But 3-5 were already selected so the real change is only 6-8 so why is the event telling me that the range 3-8 has been changed? It is even more confusing when you change the insertIndexInterval call to this:

model.insertIndexInterval(3, 3, false);  

Now the output is this:

Selected indices before insert: 3 4 5 
Changed range reported by event: 5-8
Selected indices after insert: 3 4 5 6 7 8 

I have no idea why the reported change is 5-8.

The API documentation of this method is much too short to understand what is going on there. Especially this before parameter is a mystery to me because it never has any effect on the selection but it seems to have some effect on the event and on the lead and anchor indexes.

I can’t even write a unit test for my implementation because I simply don’t know the expected results.

So can someone explain in detail what this method (And especially the beforeflag) is doing and what side effects it has on the selection model and the ListSelectionEvent?

  • 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-05-31T04:47:00+00:00Added an answer on May 31, 2026 at 4:47 am

    It is used when new data is added to the data model (so the current selection indexes should be moved). If 2 means ‘newly added and selected’ than your output would be:

    [0,0,0,1,1,1,0,0,0,0] == [3A,4,5L]
    -> add after [0,0,0,1,2,2,2,1,1,0] == [3A,4,5,6,7,8L]
    -> add before [0,0,0,2,2,2,1,1,1,0] == [3,4,5,6A,7,8L]
    

    What you see here is a feature* of DefaultListSelectionModel – adding indices inside a current selection, automatically expands the selection.

    Start with for example with index 1 selected, then insert three rows at index three:

    [1,0,0,0,0,0,0,0,0,0]
    -> add after [1,0,0,0,0,0,0,0,0,0]
    

    Note that your selection representation is misleading, the zeros aren’t really there. A better way to print the selection state would be:

    private static void printSelection(ListSelectionModel model) {
        System.out.print("[");
        for (int i = model.getMinSelectionIndex(); i <= model.getMaxSelectionIndex(); i++) {
            if(i > model.getMinSelectionIndex()) {
                System.out.print(",");
            }
            if(model.isSelectedIndex(i)) {
                System.out.print(i);
                if(i == model.getAnchorSelectionIndex()) {
                    System.out.print("A");
                }
                if(i == model.getLeadSelectionIndex()) {
                    System.out.print("L");
                }
            }
        }
        System.out.println("]");
    }
    

    *) The documentation of DefaultListSelectionModel#insertIndexInterval differs from the interface, see also https://bugs.java.com/bugdatabase/view_bug?bug_id=4700643 and http://java.net/jira/browse/SWINGX-272

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported

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.