Question

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?

Was it helpful?

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.

OTHER TIPS

There is no mouseover listener in JToolBar

You would use a MouseListener is handle the mouseEntered and mouseExited events.

But you will have a problem because the mouse events will only be passed to a visible component. So once you hide the toolbar is will not receive the mouseEntered event.

So I don't understand your design. Do you plan to have the other components shift up to fill the space by the toolbar? Or will you just leave the space empty? In the latter case you would then need to add the MouseMotionListener to the panel and handle the mouseMoved event to see the the mouse is at a location where the toolbar should be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top