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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:37:30+00:00 2026-05-21T21:37:30+00:00

I have a recurring problem where I have a JList which I wish to

  • 0

I have a recurring problem where I have a JList which I wish to update with new contents. I’m using a DefaultListModel which provides methods for adding new content to the list but when using these methods I find that some proportion of the calls result in a completely blank JList. Whether or not the update works seems to be random, and not related to the data which is sent.

Below is a simple program which demonstrates the problem. It simply generates a list of an increasing size to update a JList, but when run the list contents appear and disappear seemingly at random.

As far as I can tell I’m following the correct API to do this, but I guess there must be something fundamental I’m missing.

import java.awt.BorderLayout;
import javax.swing.*;

public class ListUpdateTest extends JPanel {

    private JList list;
    private DefaultListModel model;

    public ListUpdateTest () {
        model = new DefaultListModel();
        list = new JList(model);

        setLayout(new BorderLayout());

        add(new JScrollPane(list),BorderLayout.CENTER);
        new UpdateRunner();
    }

    public void updateList (String [] entries) {
        model.removeAllElements();
        for (int i=0;i<entries.length;i++) {
            model.addElement(entries[i]);
        }
    }

    private class UpdateRunner implements Runnable {

        public UpdateRunner () {
            Thread t = new Thread(this);
            t.start();
        }

        public void run() {

            while (true) {
                int entryCount = model.size()+1;

                System.out.println("Should be "+entryCount+" entries");

                String [] entries = new String [entryCount];

                for (int i=0;i<entries.length;i++) {
                    entries[i] = "Entry "+i;
                }

                updateList(entries);

                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException e) {}
            }
        }   
    }

    public static void main (String [] args) {

        JDialog dialog = new JDialog();
        dialog.setContentPane(new ListUpdateTest());
        dialog.setSize(200,400);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setModal(true);
        dialog.setVisible(true);
        System.exit(0);
    }

}

Any pointers would be very welcome.

  • 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-21T21:37:31+00:00Added an answer on May 21, 2026 at 9:37 pm

    Look at this code:

    import java.awt.BorderLayout;
    import javax.swing.*;
    import javax.swing.SwingWorker;
    import java.util.Arrays;
    import java.util.List;
    public class ListUpdateTest extends JPanel {
    
        private JList list;
        private DefaultListModel model;
    
        public ListUpdateTest () {
            model = new DefaultListModel();
            list = new JList(model);
    
            setLayout(new BorderLayout());
    
            add(new JScrollPane(list),BorderLayout.CENTER);
            (new UpdateRunner()).execute();
        }
    
        public void updateList (List<String> entries) {
            model.removeAllElements();
            for (String entry : entries) {
                model.addElement(entry);
            }
        }
        private class UpdateRunner extends SwingWorker<List<String>, List<String>>{
    
            @Override
            public List<String> doInBackground() {
                while (true) {
                    int entryCount = model.size()+1;
    
                    System.out.println("Should be "+entryCount+" entries");
    
                    String [] entries = new String [entryCount];
    
                    for (int i=0;i<entries.length;i++) {
                        entries[i] = "Entry "+i;
                    }
    
                    publish(Arrays.asList(entries));
    
                    try {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e) {}
                }
                return null;
            }
            @Override
            protected void process(List<List<String>> entries) {
                for (List<String> entry : entries) {
                    updateList(entry);
                }
            }
            @Override
            protected void done() {
                updateList(Arrays.asList("done"));
            }
        }
    
        public static void main (String [] args) {
    
            JDialog dialog = new JDialog();
            dialog.setContentPane(new ListUpdateTest());
            dialog.setSize(200,400);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setModal(true);
            dialog.setVisible(true);
            System.exit(0);
        }
    
    }
    

    Implemented by SwingWorker. Works smooth.

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

Sidebar

Related Questions

I enjoy developing algorithms using the STL, however, I have this recurring problem where
I have a recurring problem when passing Serialized objects between non-.NET Clients, and .NET
I'm looking for a solution to a recurring problem I have with form based
I have had this recurring problem with all of my spreadsheets for months. The
I have a problem that Curiously Recurring Templates could help quite nicely, but I
I have encountered a problem with Paypal recurring payments. CreateRecurringPaymentsProfile() returns ID of created
I have a recurring problem in templating projects. I can't really test my work
I have a reacurring problem. I code nice standards compliant code only to have
I have a problem returning data back to the function I want it returned
In a Java web application I have a recurring message load of type A

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.