Hi,
i have two buttons:
Add button and Clear button
one Jlist

1- when i click on Add button, it will add some data in the jlist.
2- when i click on Clear Button, it will clear perfectly the jlist.

but when i re-click on Add button, here it add data + the old data which was added before clearing the jlist.

how can i resolve this problem.

here is the code for the Add button:

private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {                                           
    String name = jTextField1.getText();
    currentPanel.createLayer(name);

    ArrayList<String> names = new ArrayList<String>();
    layerModel.clear();
    for(Layer layer : LayerManager.getInstance().getLayers()){
        layerModel.addElement(layer);
        names.add(layer.getName());
    }
    MatrixLayer ml = LayerManager.getInstance().getMatrixLayer();
    if(ml != null){
        for(String s : ml.getNames()){
            layerModel.addElement("Matrix : "+s);
        }
    }
    jList1.repaint();
    jPanelVizualisationOptions1.update();
    currentPanel.refresh();
}

and here the code for Clear Button:

private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {

   DefaultListModel listmodel=(DefaultListModel)jList1.getModel();
   if(evt.getSource()==jButtonClear){  
   listmodel.clear();
    }
    }

Thank you

  • for better help sooner post an SSCCE

  • short, runnable, compilable, just to avoids any code came from Layer, MatrixLayer, jPanelVizualisationOptions1.update();, currentPanel.refresh();

  • jList1.repaint(); is useless methods, sure required in the case that

    1. JList isn't inside JScrollPanel, then to call revalidate & repaint to the JLists parent

    2. have got issue with Concurency in Swing

The jList1 is inside a jPanel so if use the revalidate() here it still not working

OK, after searching in google, i did not find any solution for my problem.
but i think the main bug is in jButtobAdd function because it adds some layers and the jButtonClear is doing his work for only clearing the jList1.

Now i don't know how to avoid to add layers, this is my goal.
Do i need to change in jButtonAdd?
i tried this :

ArrayList<String> names = new ArrayList<String>();
layerModel.clear();
for(Layer layer : LayerManager.getInstance().getLayers()){
layerModel.removeAllElements();
names.remove(layer.getName());
}
MatrixLayer ml = LayerManager.getInstance().getMatrixLayer();
if(ml != null){
for(String s : ml.getNames()){
layerModel.removeAllElements();

But instead i have the :getInstance and getMatrixLayer i will never come out of the loop.
Do you have any idea?

Thank you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.