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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:37:17+00:00 2026-06-15T22:37:17+00:00

I have a JList in my Java Swing application and when a user clicks

  • 0

I have a JList in my Java Swing application and when a user clicks a button, the list clears and the contents reset as follows:

public void reset(ArrayList<String> content) {
    listModel.removeAllElements();
    System.out.println(content.size());
    for(int i = 0; i < content.size(); i++) {
       listModel.addElement(content.get(i));
       System.out.println("Added element " + content.get(i));
   }
}

The list is initialized as follows

listModel = new DefaultListModel();
list = new JList(listModel);

However there is a problem. The list clears (before the reset, there was other content. This content does go away), but the new content does not show up. And, from the output, I can see that 6 elements have been added. But they do not show up in the list. Why is this?

  • 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-15T22:37:18+00:00Added an answer on June 15, 2026 at 10:37 pm

    However there is a problem. The list clears (before the reset, there
    was other content. This content does go away), but the new content
    does not show up. And, from the output, I can see that 6 elements have
    been added. But they do not show up in the list. Why is this?

    nobody knows, only Concurency in Swing could be issue, be sure that all events would be dode on EDT, otherwise for better help sooner post an SSCCE, for example

    EDIT

    added String instances requested by @Robin

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class ListString extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private DefaultListModel model = new DefaultListModel();
        private int i = 01;
    
        public ListString() {
            model.addElement(("one" + i++));
            model.addElement(("two" + i++));
            model.addElement(("three" + i++));
            model.addElement(("four" + i++));
            JList list = new JList(model);
            add(new JScrollPane(list));
            JButton btn = new JButton("Remove All Rows :");
            btn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    model.removeAllElements();
                }
            });
            add(btn, BorderLayout.SOUTH);
            JButton btn1 = new JButton("Add New Rows:");
            btn1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    model.addElement(("one" + i++));
                    model.addElement(("two" + i++));
                    model.addElement(("three" + i++));
                    model.addElement(("four" + i++));
                }
            });
            add(btn1, BorderLayout.NORTH);
        }
    
        public static void main(String[] args) {
            UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
            ListString frame = new ListString();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    }
    

    with rendering JPanel inside JScrollPane requested by @trashgod

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    
    public class ListPanel extends JFrame {
    
        private static final long serialVersionUID = 1L;
        private DefaultListModel model = new DefaultListModel();
        private int i = 01;
    
        public ListPanel() {
            model.addElement(createPanel("one" + i++));
            model.addElement(createPanel("two" + i++));
            model.addElement(createPanel("three" + i++));
            model.addElement(createPanel("four" + i++));
            JList list = new JList(model);
            list.setCellRenderer(new PanelRenderer());
            add(new JScrollPane(list));
            JButton btn = new JButton("Remove All Rows :");
            btn.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    model.removeAllElements();
                }
            });
            add(btn, BorderLayout.SOUTH);
            JButton btn1 = new JButton("Add New Rows:");
            btn1.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    model.addElement(createPanel("one" + i++));
                    model.addElement(createPanel("two" + i++));
                    model.addElement(createPanel("three" + i++));
                    model.addElement(createPanel("four" + i++));
                }
            });
            add(btn1, BorderLayout.NORTH);
        }
    
        public static JPanel createPanel(String text) {
            JPanel panel = new JPanel();
            panel.add(new JLabel("Item: "));
            panel.add(new JLabel(text));
            return panel;
        }
    
        public static void main(String[] args) {
            UIManager.getLookAndFeelDefaults().put("List.selectionBackground", Color.red);
            ListPanel frame = new ListPanel();
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
            frame.pack();
            frame.setVisible(true);
        }
    
        private class PanelRenderer implements ListCellRenderer {
    
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                JPanel renderer = (JPanel) value;
                renderer.setBackground(isSelected ? list.getSelectionBackground() : list.getBackground());
                return renderer;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have a JList listen to list selection listener. private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt)
I'm making a swing based application where I have a JList that periodically get's
On my Java Swing application I have two components. On the left side is
I have a simple java swing app, built in netbeans. There's a JList in
I have a JList, and a JButton, user can click an item in the
I have a JList inside a Scrollpane. If you click on the list and
Made a custom ListCellRenderer: import java.awt.Component; import javax.swing.JCheckBox; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JPanel;
I made a simple Swing application. But the rendering behaves buggy. Have I done
I have made a java GUI program and have added a jList on that
In Java Swing, what's the best way for a JList and a JComboBox to

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.