How to clear a JList in Java?
Asked Answered
F

4

21

i have a jList in gui where i can add some data with Add button. what i want to add another button called Clear which will clear all elements. i tried this:

private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt)
{
    DefaultListModel listmodel=new DefaultListModel();
    jList1 = new JList(listmodel);
    if(evt.getSource()==jButtonClear) JList.setListData(new String[0];
    else listmodel.removeAllElements();
}

When I click on Add button this will add elements.

When I click on Clear button this remove elements.

But when I re-click on Add button, there is nothing in the jList1

Faviolafavonian answered 28/11, 2012 at 4:35 Comment(0)
H
29

You should not be reinitializing the entire JList widget just to remove some items from it. Instead you should be manipulating the lists model, since changes to it are 'automatically' synchronized back to the UI. Assuming that you are indeed using the DefaultListModel, this is sufficient to implement your 'Clear All' functionality:

private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt) {
    if(evt.getSource()==jButtonClear) {
        DefaultListModel listModel = (DefaultListModel) jList1.getModel();
        listModel.removeAllElements();
    }
}
Hawn answered 28/11, 2012 at 4:50 Comment(0)
Q
8

There are number of problems, the first been that your example is full of compile problems, so I hope that's not your actual code.

JList does not have static method called setListData. I think you mean jList1 instead.

Each time you click on the clean button, you are creating a new model and component...

private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt)
{
    // ??
    DefaultListModel listmodel=new DefaultListModel();
    jList1 = new JList(listmodel);
    // ??
    if(evt.getSource()==jButtonClear) jList1.setListData(new String[0]);
    else listmodel.removeAllElements();
}

You've successfully dereferenced what ever jList1 was pointing at, so any time you try and interact with it, you're no longer interacting with the component on the screen.

The other problem is you supplying a empty array to the setListData method, which basically is like saying, "please add nothing to my list"

Try something like this;

private void jButtonClearActionPerfomed(java.awt.event.ActionEvent evt)
{
    DefaultListModel listmodel = (DefaultListModel)jList1.getModel();
    if(evt.getSource()==jButtonClear) {
        listmodel.removeAllElements();
    } else {
        listModel.addElement(new String[]{"Hello"});
    }
}
Quadrature answered 28/11, 2012 at 4:48 Comment(3)
ok for the copmile errors with your modification, now when i click the Clear button it adds some new datas which i don't know it comes from...But i try this and it clears all data but when i click the Add button it adds new data but it keeps the old data before clearing: code : DefaultListModel listmodel=(DefaultListModel)JList1.getModel(); evt.getSource()==jButtonClear{ listmodel.removeAllElements();Faviolafavonian
I've switch the conditions (so that clear now calls removeAll) otherwise it will add a new element. I stole your code and wasn't careful about fixing it. I'm not sure if this is shared code with another action event or not. If not, remove the addElement lineQuadrature
yes it's a shared code with the Add button, i can't put the code here because i'm new here, but here is what is at the end of the code of Add button: jList1.repaint(); jPanelVizualisationOptions1.update(); currentPanel.refresh();Faviolafavonian
A
3

try this:

DefaultListModel model = new DefaultListModel();
model.clear();
jList1.setModel(model);
Aqualung answered 19/10, 2017 at 22:56 Comment(0)
A
1

try this one:

DefaultListModel listmodel=new DefaultListModel();

JList.setModel(listmodel);
Astarte answered 19/11, 2020 at 23:6 Comment(1)
Hi Nathanzkie. Welcome to Stack Overflow & thank you for your contribution! But while this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Tips for Answers. Kind Regards.Trine

© 2022 - 2024 — McMap. All rights reserved.