views:

529

answers:

1

Is there any way of removing the Component added to the CENTER of a JPanel with a BorderLayout, without having to reference the Component itself?

+3  A: 

Something like this?

BorderLayout layout = panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
ninesided
Cool. Had to modify it a bit, but it worked.Used it in this fashion: myPanel.remove(layout.getLayoutComponent(BorderLayout.CENTER);
Evan Fosmark
nice one, I've updated my answer to reflect that change
ninesided
You're both missing the closing parenthesis for the remove function ;)
Tom Martin
Ensure you you call Container.validate() after you change the components to signal layout manager to recalculate the positions of the components being displayed. In your example it would be panel.validate();
gencoreoperative