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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:14:56+00:00 2026-06-15T11:14:56+00:00

To preface, I realize this is probably an Event Dispatch Thread issue. I am

  • 0

To preface, I realize this is probably an Event Dispatch Thread issue. I am just unsure where exactly the issue lies.

I have a JList that uses an AbstractListModel to query an object and display the results as a list:

public class View {

    public View(final Person person) {
        JList list = new JList(new AbstractListModel() {

            @Override
            public Object getElementAt(int index) {
                return person.getSibling(index);
            }

            @Override
            public int getSize() {
                return person.getNumSiblings();
            }

        });
 }

Initially, the JList looks fine – it displays the one sibling that all “Person”s are automatically constructed with. However, when I use something like person.addSibling(…) in another area of my code then the JList turns blank.

Is this an EDT issue? (It appears that after updating a Person the AbstractListModel’s methods are no longer called.)

If so, where should I add the SwingWorker code – inside of the AbstractListModel or inside of the person.addSibling(…)?

Thanks!

EDIT:

I am appending a simple, runnable version:

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Test {

    public static void main(String[] args) {
        final Person person = new Person();

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.setPreferredSize(new Dimension(1024, 768));
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);

                    JPanel panel = new JPanel();
                    frame.setContentPane(panel);

                    JList list = new JList(new AbstractListModel() {

                        @Override
                        public Object getElementAt(int index) {
                            return person.getSibling(index);
                        }

                        @Override
                        public int getSize() {
                            return person.getNumSiblings();
                        }

                    });
                    panel.add(list);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);

                    person.addSibling("Bob");
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
        });


    }

    private static class Person {

        List<String> siblings = new ArrayList<String>();

        public Person() {
            siblings.add("Janice");
        }

        public void addSibling(String sibling) {
            siblings.add(sibling);
        }

        public Object getSibling(int index) {
            return siblings.get(index);
        }

        public int getNumSiblings() {
            return siblings.size();
        }

    }

}
  • 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-15T11:14:58+00:00Added an answer on June 15, 2026 at 11:14 am

    This has nothing to do with the EDT or threading. You’re not calling any of the model’s notification methods, fireXXXX(...) when the data nucleus of the model changes, and this is messing it up. I suggest you fix this. the AbstractListModel API will show the methods that are available for when data is added or removed from the list.

    Edit 1
    Also your model should be in a non-anonymous class that extends AbstractListModel<String> and that has an addSibling(String sib) method. You should add the sibling to the model not the Person object.

    Edit 2 e.g.,

    import java.util.List;
    
    import javax.swing.AbstractListModel;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    
    public class Test {
    
       public static void main(String[] args) {
          final Person person = new Person();
          final SibListModel listModel = new SibListModel(person);
    
          SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {
                try {
                   JFrame frame = new JFrame();
                   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   JPanel panel = new JPanel();
                   frame.setContentPane(panel);
    
                   JList<String> list = new JList<String>(listModel);
                   panel.add(new JScrollPane(list));
                   frame.pack();
                   frame.setLocationRelativeTo(null);
                   frame.setVisible(true);
    
                   // person.addSibling("Bob");
                   listModel.addSibling("Bob");
                } catch (Throwable ex) {
                   ex.printStackTrace();
                }
             }
          });
    
       }
    
       private static class SibListModel extends AbstractListModel<String> {
          private Person person;
    
          public SibListModel(Person person) {
             this.person = person;
          }
    
          @Override
          public String getElementAt(int index) {
             return person.getSibling(index);
          }
    
          @Override
          public int getSize() {
             return person.getNumSiblings();
          }
    
          public void addSibling(String sib) {
             person.addSibling(sib);
             fireIntervalAdded(this, person.getNumSiblings() - 1, person.getNumSiblings());
          }
       }
    
       private static class Person {
    
          List<String> siblings = new ArrayList<String>();
    
          public Person() {
             siblings.add("Janice");
          }
    
          public void addSibling(String sibling) {
             siblings.add(sibling);
          }
    
          public String getSibling(int index) {
             return siblings.get(index);
          }
    
          public int getNumSiblings() {
             return siblings.size();
          }
    
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll preface this with the background that I am very green and don't have
Preface: I am sure this is incredibly simple, but I have searched this site
I will preface this by I am new to XNA and I realize what
Preface: This is not much about how to structure code within files. I have
Preface: This is the first real swing program I have done. I have a
Let me preface this by saying I now realize how stupid I am and
To preface this question, I have a competent understanding of OpenGL and the maths
Preface: I have looked all over stackoverflow.com and google for this. I have found
Preface: I'm a total Java noob...I just wrote Hello World yesterday. Have mercy on
To preface this question, this is my first app and I have a very

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.