1,105,334 Community Members

How to clear a Jlist shared with another code

Member Avatar
Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
mKorbel
Nearly a Posting Virtuoso
1,345 posts since Feb 2011
Reputation Points: 274 [?]
Q&As Helped to Solve: 277 [?]
Skill Endorsements: 14 [?]
 
0
 
  • 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

Member Avatar
Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article