When adding / removing components from the visible GUI, the code should look like this:
panel.remove(...); panel.add(...); panel.revalidate(); panel.repaint();
In addition, the code must run in Thread Dispatch Thread . If the code is executed from a listener, it is on the EDT. If the code is running outside the listener, then you probably have to use SwingWorker or SwingUtilities.invokeLater ().
source share