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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:07:02+00:00 2026-05-24T05:07:02+00:00

I’ve noticed is that when an element is added to a JList and the

  • 0

I’ve noticed is that when an element is added to a JList and the element happens to fall within a range of values that are selected, it ends up being selected by default. In fact, if an element is added just above a selected value it is added to the selection.

I’ve tried looking at the JList code (in the open JDK6) and the BasicListUI code, but I’m having trouble teasing out the details of why this happens or what I need to do to fix it.

I’m considering supplying a custom SelectionModel, as that would be a sensible place to do some of the other work in my application, but I’m afraid that might make the problem worse, or harder to fix. Does anyone know why this happens? What I can do to fix it?

I’ve created this example that demonstrates the issue:

package jlistsscce;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;

public class JListSSCCE extends JPanel
{
    private final JList list;
    private ScheduledExecutorService ses;

    public JListSSCCE()
    {
        list = new JList();
        list.setModel(new DefaultListModel());
        ses = Executors.newSingleThreadScheduledExecutor();
        ses.scheduleAtFixedRate(new NewElement(), 100, 100, TimeUnit.MILLISECONDS);
        add(list);
    }

    private static void createGui()
    {
        // create new JFrame
        JFrame jf = new JFrame("JListSSCCE");

        // this allows program to exit
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // You add things to the contentPane in a JFrame
        jf.getContentPane().add(new JListSSCCE());

        // size frame
        jf.pack();

        // make frame visible
        jf.setVisible(true);

    }

    public static void main(String[] args)
    {
        // threadsafe way to create a Swing GUI
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run()
                {
                    createGui();
                }
            }
        );
    }

    private class NewElement implements Runnable
    {
        int n = 0;
        @Override
        public void run()
        {
            ((DefaultListModel)list.getModel()).add((int)Math.floor(Math.sqrt(n)), ("hey"+n));
            n++;
        }

    }
}
  • 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-24T05:07:02+00:00Added an answer on May 24, 2026 at 5:07 am

    This isn’t the problem but I believe you should be using a Swing Timer instead of an Executor so the code get executed on the EDT.

    Yes the problem is the selection model. The last time I looked at the code I found it rather confusing, so I’m not sure you want to play with it.

    The only thing I can think of is to make sure you are using the multiple selection interval. Then after inserting an elemnt you check to see if the element is selected. If it is then you remove the selection for that element.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.