Hiding a JToolBar that has been made floatable

Here’s an example that will hide a JToolBar, whether or not they have been made floatable.

Main.java:

import javax.swing.border.*;
import javax.swing.plaf.basic.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
  
public class Main extends JFrame {
   public Main() {
      super("JToolBar hide/show example");
 
      final JTextArea textArea = new JTextArea(5, 30);
      final MyJToolBar toolBar = new MyJToolBar();
 
      JButton gifButton = new JButton(new ImageIcon("gifIcon.gif"));
      JButton jpgButton = new JButton(new ImageIcon("jpgIcon.gif"));
      JButton tiffButton = new JButton(new ImageIcon("tiffIcon.gif"));
 
      gifButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            textArea.append("gifButton clicked!n");
         }
      });
  
      jpgButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            textArea.append("jpgButton clicked!n");
         }
      });
        
      tiffButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            textArea.append("tiffButton clicked!n");
         }
      });
 
      toolBar.add(gifButton);
      toolBar.add(new JToolBar.Separator());
      toolBar.add(jpgButton);
      toolBar.add(new JToolBar.Separator());
      toolBar.add(tiffButton);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
 
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(BorderLayout.NORTH, toolBar);
      getContentPane().add(BorderLayout.CENTER, new JScrollPane(textArea));
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Toolbar");
      final JMenuItem hideShowMenuItem = new JCheckBoxMenuItem("Hide");
      menu.add(hideShowMenuItem);
      menuBar.add(menu);
      setJMenuBar(menuBar);
   
      hideShowMenuItem.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            toolBar.setVisible(!hideShowMenuItem.isSelected());
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}
 
class MyJToolBar extends JToolBar
{
   public MyJToolBar() {
      super();
   }
 
   public MyJToolBar(int orientation) {
      super(orientation);
   }
 
   public MyJToolBar(String name) {
      super(name);
   }
 
   public MyJToolBar(String name, int orientation) {
      super(name, orientation);
   }
 
   public void setVisible(boolean visible) {
      boolean isFloating = false;
      if (getUI() instanceof BasicToolBarUI) {
         isFloating = ((BasicToolBarUI) ui).isFloating();
      }
 
      if (isFloating) {
         SwingUtilities.getRoot(this).setVisible(visible);
      }
      else {
         super.setVisible(visible);
      }
   }
}