Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem clearing my ArrayList, a ghost is filling it up again.

 
Alexander Selling
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

So the purpose of this program is to display the correct children to the parent you click on, for instance if you click on "room" you should get a couple of children named "wall", "floor", "ceiling" and so on..
What I want is, when you click on room all the other options that were available should disappear and only the children of chosen object should appear.
Now I've almost got it all to work, except for the ArrayList of strings called "strn".

First you click on a checkbox, getQuery is called and returns the children of that checkbox. Now the arraylist strn is filled with these children
and is sent off to the method called generateMenu, the checkboxes are generated. Up til this point everything is fine, however the next time this happens it generates the old and the new checkboxes, in other words the parents and the children into a big mess. I've tried to create a new arraylist, I've tried to use strn.clear(), I've tried to use strn.removeAll(strn). But it still somehow remembers what the old objects are ?

It's so weird to look at my little test code:

System.out.println(strn.toString());
strn.removeAll(strn);
System.out.println(strn.toString());
The first print line gives a full array and the 2nd gives an empty one, so it is empty ? But somehow it stores them and remembers to put them back in there when the method is called the next time.
And btw I've had all sorts of diff ways to clear/remove the array this is just one of them.


 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the returned value of getQuery ? Is it ok ? Isn't getQuery returning you too many results ?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
updateUI() isn't the correct method to invoke after adding/removing components from a visible container. Use revalidate() and repaint() instead.
 
Alexander Selling
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:What about the returned value of getQuery ? Is it ok ? Isn't getQuery returning you too many results ?


Thanks a bunch, I never cleared the arraylist that the class Query fills the "queries" with. Now it's all working.

Darryl Burke wrote:updateUI() isn't the correct method to invoke after adding/removing components from a visible container. Use revalidate() and repaint() instead.


I've changed now, but what is the difference ?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the documentation for updateUI()
Notification from the UIManager that the L&F has changed.

And for revalidate()
Supports deferred automatic layout.

Which looks appropriate to you?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic