I have a JButton in my application to add/remove a JToolBar. When the toolbar is docked, I can remove it using:
toolBar.getParent().remove(toolbar); // toolBar is an instance of JToolBar
If I do this while the toolbar is floating, nothing happens, the floating toolbar remains there.
Using the BasicToolBarUI object obtained from the JToolBar, we can know if it floating. If the JToolbar is floating, we can dispose its window.