How to reset JList?

how to clear all items inside a JList?

I have tried using


but does no effect.

I also tried using code below:

Object[] blank = {""};

.. it will clear up, but change this list as if setVisibleRowCount = 1.

Thanks & regards

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Here is an example application of JList (hand-made!):

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

public class Listing
    extends JFrame
    implements ActionListener
    private JButton butAdd;
    private JButton butClear;
    private JList list;
    private DefaultListModel model;
    private int num;

    public Listing()
        this.setLayout(new FlowLayout());
        this.setTitle("List Example");

        this.num = 1;

        this.model = new DefaultListModel();

        this.butClear = new JButton("Clear!");

        this.butAdd = new JButton("Add");

        this.list = new JList(model);


    public void actionPerformed(ActionEvent ae)
        if(ae.getSource() == this.butClear)
        else if(ae.getSource() == this.butAdd)
            this.model.add(this.model.getSize(),"Track "+this.num);

    public static void main(String[] args)
        new Listing().setVisible(true);

As you can see, it works with a DefaultListModel, where you have a clear() method to clear the list.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I also have a probable cause of your problem, the GUI doesn't get updated after a "removeAll()", so maybe all the elements are removed, but the GUI doesn't know yet. It might work if you do a repaint().

isuhendroAuthor Commented:
hello Mark,

thanks for your reply. I prefer not to using model for this JList, otherwise will need to change codes at other part.

btw repaint() will not help as well..
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Well, then you are bound to the solution you mentioned: = new String[]{};

Set the list data to be empty. Don't forget to also set your real data holder (if it's an attribute of your class) to empty, otherwise when you reuse the variable to reset the listdata, it will still contain the old values.

isuhendroAuthor Commented:
ok then Mark... just found out that the problem with setVisibleCountRow become 1, is related to the BorderLayout... not to the list itself.. so i will use previous solution. Btw what is the advantage of using DefaultListModel ?
Thanks & regards.
Better design, more functionality for free.
Take a look at the model-view-controller design pattern. You have one model with maybe multiple views and multiple controllers. This way the model/view/controller are separated from each other which makes extending it easy etc.
Many advantages by using established design patterns.. anyway, it's not necessary. :-)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.