<Swing Dev> adding a component to a container that it is already a member of (specifically a JLayeredPane container)
Alan Snyder
javalists at cbfiddle.com
Thu Oct 3 19:16:22 UTC 2019
The Container.addImpl method, although protected, defines the behavior of adding a component to a container. The documentation for this method says:
If the component is not an ancestor of this container and has a non-null parent, it is removed from its current parent before it is added to this container.
That would suggest that adding a component to its current parent is supported usage.
However, this usage is not supported by JLayeredPane. The problem is that when adding a component, JLayerPane computes the insertion position of the component based on the current list of child components. It does not take into account the possibility that the component is already present and will be removed *after* the new position has been computed. The consequence is that the component may be positioned incorrectly. (This happened to me.)
This problem was reported back in 1998 (see JDK-4107649 <https://bugs.openjdk.java.net/browse/JDK-4107649> [1]), but the conclusion was:
This behavior is not supported, and it is highly unlikely we will fix this.
I do not agree with this conclusion, as it is inconsistent with the documentation of Container.
I also note that the obvious workaround, removing before adding, has disadvantages, as described in the evaluation of JDK-6185498 <https://bugs.openjdk.java.net/browse/JDK-6185498> [2].
A better implementation would rearrange the components (if needed) rather than removing and adding.
If this case is truly not supported, then the documentation should be improved to make it clear that this case is not supported.
Which direction should be taken? Fix the code or the documentation?
Alan
[1] https://bugs.openjdk.java.net/browse/JDK-4107649 <https://bugs.openjdk.java.net/browse/JDK-4107649>
[2] https://bugs.openjdk.java.net/browse/JDK-6185498 <https://bugs.openjdk.java.net/browse/JDK-6185498>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.java.net/pipermail/swing-dev/attachments/20191003/1333d5b4/attachment.html>
More information about the swing-dev
mailing list