30

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?

ninesided
  • 23,085
  • 14
  • 83
  • 107
Evan Fosmark
  • 98,895
  • 36
  • 105
  • 117

1 Answers1

50

Something like this?

BorderLayout layout = (BorderLayout)panel.getLayout();
panel.remove(layout.getLayoutComponent(BorderLayout.CENTER));
ninesided
  • 23,085
  • 14
  • 83
  • 107
  • 2
    Cool. Had to modify it a bit, but it worked. Used it in this fashion: myPanel.remove(layout.getLayoutComponent(BorderLayout.CENTER); – Evan Fosmark Apr 17 '09 at 07:40
  • You're both missing the closing parenthesis for the remove function ;) – Tom Martin Apr 17 '09 at 11:07
  • 5
    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 May 25 '10 at 12:11
  • Can someone explain where exactly you put this code or how to use it. – Jack Oct 24 '14 at 17:02
  • i get "incompatible types: LayoutManager cannot be converted to BorderLayout" error message. What i am doing wrong? – Adnan Ahmad Khan Jun 17 '15 at 15:42
  • Probably best if you post a new question along with your code, it might be a slightly different scenario – ninesided Jun 18 '15 at 07:10