Search code examples
javaswingjtoolbar

How to make an auto-hide the JToolBar?


I'd like to make an auto-hide the JToolBar and it appear only when the mouse goes near/over the JToolBar. I have added the JToolBar in JPanel. There is no mouseover listener in JToolBar. How to do this?


Solution

  • Add a MouseMotionListener to your JFrame or JDialog.

    addMouseMotionListener(new MouseAdapter() {
        public void mouseMoved(MouseEvent e) {
             toolbar.setVisible(e.getY() < 10);
         }
    });
    

    In that way, the tool bar will only be shown if the mouse is in the top 10 vertical pixels of the window.