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?

- 14,283
- 16
- 57
- 95
-
1can you please post SSCCE maybe there no needed apply the `MouseWhatever` – mKorbel Nov 17 '11 at 15:50
2 Answers
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.

- 428
- 3
- 3
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.

- 321,443
- 19
- 166
- 288
-
1+1 `because the mouse events will only be passed to a visible component`, – mKorbel Nov 17 '11 at 17:03