Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

adding and removing items to a JPanel with BorderLayout

810185Nov 3 2010 — edited Nov 3 2010
I have a JPanel and have set the layout manager to "BorderLayout".

I add a JMenu using "theJPanel.add(firstJMenu,BorderLayout.NORTH);"

and another JPanel using "theJPanel.add(subJPanel,BorderLayout.CENTER);"

Then, I want to change the contents of the two positions. I do

theJPanel.removeAll();
theJPanel.add(newJMenu,BorderLayout.NORTH);
theJPanel.add(newJPanel,BorderLayout.CENTER);
theJPanel.validate();

Things MOSTLY work great but, occasionally, I get a exception during repaint "IndexOutOfRange" and "no child 1".

It appears that the 'remove' is not synchronized with the repaint walking the component subelements. I use eclipse and walk through the remove and things appear synchronized........

I would have expected this to be a common programming style. Is this just a wrong headed approach?

How can I have the same effect?

My thanks,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2010
Added on Nov 3 2010
3 comments
1,187 views