Container.remove issue

Alan Snyder javalists at cbfiddle.com
Mon Nov 6 19:53:47 UTC 2023


I have experienced a problem related to Container.remove(index).

As this code has been around for a long time, I’m wondering (1) why it does what it does and (2) whether
there is some AWT/Swing design rule that I should be following (but I don’t know of one that would have helped).

The implementation of Container.remove(index) first identifies the component at the specified index in the component list.

It then calls removeNotify on that component and calls removeLayoutComponent on its layout manager.

Only then does it remove the component from the component list.

The problem is that is uses the original index to remove the component from the component list, which
presumes that the previous calls to removeNotify and removeLayoutComponent have not done anything
to alter the component list.

As both of these methods can run arbitrary client code, the presumption seems unwarranted.

In my case, removeNotify eventually was called on a focused text field, which caused the text field
to lose focus, which generated events, and an event handler removed the same component from the
container.

What am I missing?



More information about the client-libs-dev mailing list