How to make JToolBar auto-hide?

I would like to make auto-hide a JToolBar , and it only appears when the mouse is approaching / above the JToolBar . I added JToolBar to JPanel . There JToolBar no mouse listener in JToolBar . How to do it?

+4
source share
2 answers

Add MouseMotionListener to your JFrame or JDialog .

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

Thus, the toolbar will be displayed only if the mouse is in the top 10 vertical pixels of the window.

+4
source

There is no mouse listener in JToolBar.

You would use MouseListener to handle mouseEntered and mouseExited .

But you will have a problem, because mouse events will be passed only to the visible component. Therefore, as soon as you hide the toolbar, you will not receive the mouseEntered event.

Therefore, I do not understand your design. Are you planning to move other components to fill the space on the toolbar? Or do you just leave empty space? In the latter case, you will need to add MouseMotionListener to the panel and handle the mouseMoved event to see that the mouse is in the place where the toolbar should be.

+3
source

Source: https://habr.com/ru/post/1381719/


All Articles