I used this answer to add buttons to my GUI dynamically and expected to be able to remove all of them too. To my understanding I am getting all the keys in the HashMap (strings) and then I am doing a for loop over the keys, and deleting them from the hashmap (getting the object back which I will delete). Problem is that after deleting the first button from the hashmap, the loop doesn't continue and my application crashes.
HashMap<String, JButton> buttonCache = new HashMap<>();
Set<String> names = buttonCache.keySet();
/*
* Checking which buttons exist in the hashmap
*/
for (String name0 : names) {
System.out.println("Name0: " + name0);
}
for (String name1 : names) {
System.out.println("before removing: " + name1);
buttonCache.containsKey(name1); //making sure its in it.
JButton b = buttonCache.remove(name1);
System.out.println("after removing: " + name1);
//visualUI.remove(b); //not tested yet
}
//visualUI.invalidate(); //not tested yet
//visualUI.repaint(); //not tested yet
The output is:
Name0: Cancel
Name0: Continue
2
before removing: Cancel
true
after removing: Cancel