Auto-hide JMenuBar
Posted
by PeterMmm
on Stack Overflow
See other posts from Stack Overflow
or by PeterMmm
Published on 2010-05-26T10:02:05Z
Indexed on
2010/05/26
10:21 UTC
Read the original article
Hit count: 328
When i run the code above the frame's menu bar come up when the mouse moves to the upper part of the window. The problem is when i open the menu but do not select any item and move out the mouse the menu bar get invisible but the items stay on screen.
public class Test extends JFrame {
public Test() {
setLayout(new BorderLayout());
setSize(300, 300);
JMenuBar mb = new JMenuBar();
setJMenuBar(mb);
mb.setVisible(false);
JMenu menu = new JMenu("File");
mb.add(menu);
menu.add(new JMenuItem("Item-1"));
menu.add(new JMenuItem("Item-2"));
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
getJMenuBar().setVisible(e.getY() < 50);
}
});
}
public static void main(String args[]) {
new Test().setVisible(true);
}
}
© Stack Overflow or respective owner