While Carl's answer is probably the best, less pleasant alternative, if for some reason you cannot change the original add () call:
contents.remove(((BorderLayout)getLayout()).getLayoutComponent(BorderLayout.CENTER)); contents.add(someNewPanel);
Although, if you think you need to do this, you may need to step back and evaluate why you are trying to do this.
source share