3

I am trying to clear an HBox from its items. I tried using hBox.getChildren().clear(), and even looping

for(Node child: hBox.getChildren()){
    hBox.getChildren().remove(child);
}

but neither one works. Any ideas?

melkhaldi
  • 899
  • 2
  • 19
  • 40

1 Answers1

1

The remove version will give you a ConcurrentModificationException - clear should just work - at least it works for me

tomsontom
  • 5,856
  • 2
  • 23
  • 21
  • 1
    But he's using `hBox.clear()`, it needs to be `hBox.getChildren().clear();`. – brian Feb 16 '14 at 22:50
  • Hi Brian, I was actually using hBox.getChildren().clear(); made a mistake in my question--just corrected it. Both do not work for me. Not sure why. I am on Java 8 build-129. – melkhaldi Feb 17 '14 at 00:40
  • Hi Tomston, i do not get any run time errors actually... :/ – melkhaldi Feb 17 '14 at 00:41
  • @melkhaldi You're probably clearing the list and adding them again somewhere in your code. I can't imagine why it's not working otherwise. Maybe try stepping through after the clear() and see what's happening. – brian Feb 17 '14 at 02:16
  • 1
    I thought about that and I did go everywhere in the code to make sure that the elements that I add are all set to not visible, removed them from the HBox. and then print the HBox.getChildren().size().. all prints to zero.. I'll spend the whole day tomorrow to figure it out with the debugger. will report back! – melkhaldi Feb 17 '14 at 04:33
  • if you don't get am CME in the above code this means the hbox has 0 or 1 child - are you sure you are operating on the correct hbox? – tomsontom Feb 17 '14 at 05:58
  • Yes there is only one HBox. However, I managed to find a line in my code caused the node to reappear. Took me 4 hours of debugging! Finlaly found it. – melkhaldi Feb 18 '14 at 00:12
  • @melkhaldi what was the bug ? i have a similar problem. – gontard Jun 20 '14 at 08:54
  • hi. there something wrong in my code actually. not a bug in java – melkhaldi Jun 20 '14 at 23:27