We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,731 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to clear a Jlist shared with another code

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

2
Contributors
3
Replies
1 Day
Discussion Span
5 Months Ago
Last Updated
4
Views
Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

mKorbel
Nearly a Posting Virtuoso
1,208 posts since Feb 2011
Reputation Points: 482
Solved Threads: 239
Skill Endorsements: 12

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

Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

Neversleepin
Junior Poster in Training
97 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.3028 seconds using 2.87MB