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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:53:25+00:00 2026-05-24T23:53:25+00:00

I am having an issue in my Swing app where I am creating a

  • 0

I am having an issue in my Swing app where I am creating a simple JList by passing it a model – and even though the model is demonstrably populated, the JList refuses to display it’s own model’s contents.

DefaultListModel dlm = new DefaultListModel();
String[] modelElems = {"Apple", "Orange", "Banana"};
for(int i = 0; i < modelElems.length; i++)
    dlm.add(i, modelElems[i]);

JList lstFruitList = new JList(dlm);
lstFruitList.setVisible(true);

When my Swing app runs, I see the JList on screen, but it is completely empty! I’ve looked at countless examples, poured over the Swing tutorials, and cannot seem to figure out what is going on. Anybody ever had this happen to them before?!? Anything that is glaringly-obviously-wrong?!?

NOTE:

The following print statement indeed shows that my model has 3 elements:

// Prints "Fruit List model has a size of 3"
System.out.println("Fruit List model has a size of " + dlm.size());

However, if I try to loop through and print the names of the fruits in my model, by calling (String)dlm.get(i) at every iteration (where i is the iteration var), it prints each model element as null…

hmmmm

  • 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-24T23:53:26+00:00Added an answer on May 24, 2026 at 11:53 pm

    That code works fine for me. Some thoughts:

    • I see that you call the setVisible on the JList, how exactly are you adding it to whatever you are displaying?
    • Are you adding the elements after you display your JFrame? If I remember correctly, that won’t work well, I think you have to repaint everything.
    • Make sure that you’re not messing around with the list model at some point; Maybe at some point later in the code, you’re changing it?
    • Ensure that you are actually looking at the right element; maybe your list is hidden behind something else?(try setting the background color, i.e. lstFruitList.setBackground(Color.BLUE);
    • Final thought, are you sure that you’ve compiled it properly? I’ve sometimes accidentally forgotten to compile, or had messed up something and was running an older version of the code and was confused as to why something didn’t work.

    For reference, here’s the code that I ran:

    import java.awt.*;
    import javax.swing.*;
    
    public class javatest{
    
        public static void main(String[] args){
                JFrame f = new JFrame("HELLO");
                DefaultListModel dlm = new DefaultListModel();
                String[] modelElems = {"Apple", "Orange", "Banana"};
                for(int i = 0; i < modelElems.length; i++)
                    dlm.add(i, modelElems[i]);
    
                JList lstFruitList = new JList(dlm);
                lstFruitList.setVisible(true);
    
                JPanel p = new JPanel();
                p.add( lstFruitList );
                f.add( p );
                f.setLocation(0,0);
                f.setSize(400,400);
                f.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a bit of an issue right now. Is there any simple
I'm making a simple game in Java using swing and am having problems with
Greetings of the Day, I am having issue in my app.Actually, in my app
I'm having some strange issues with repainting going on in a Swing app I'm
I'm having issue with a simple code downloading a remote image and adding content
I having issue that content assistant / intellisense is working in methods such as
i am having issue with loading my selector via a jquery ajax load call
Having an issue with random individuals trying to access an intranet site with a
Having an issue here that I have tried everything I can think of but
Having an issue with traversing in jQuery.. hopfully an easy one..but I'm banging my

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.