Java Container remove method not working correctly
The Container
returned by getContentPane()
is, by default, the contentPane
of a JRootPane
managed by the top-level container, JFrame
. Although, "as a convenience, the add
method and its variants, remove
and setLayout
have been overridden to forward to the contentPane
as necessary," there's no a priori way to know about the frame's internal use of component indices.
Instead, add on your own JComponent
to the frame and operate on it; JPanel
is a common choice.
Addendum: Also consider an alternative layout such as CardLayout
, illustrated here.