01: package examples.swingdemos;
02:
03: import java.awt.*;
04: import java.awt.event.*;
05:
06: import javax.swing.*;
07:
08: /** @author ges */
09:
10: public class MenuLayoutDemo extends JFrame {
11: protected JMenuBar menuBar;
12:
13: public MenuLayoutDemo() {
14: super ("MenuLayoutDemo");
15: menuBar = new JMenuBar();
16: menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.Y_AXIS));
17: addNewMenu("Menu 1");
18: addNewMenu("Menu 2");
19: addNewMenu("Menu 3");
20: getContentPane().add(menuBar, BorderLayout.WEST);
21:
22: addWindowListener(new WindowAdapter() {
23: public void windowClosing(WindowEvent e) {
24: System.exit(0);
25: }
26: });
27: }
28:
29: public void addNewMenu(String title) {
30: JMenu m = menuBar.add(new HorizontalMenu(title));
31: m.add("Menu item");
32: m.add("Menu item");
33: m.add("Menu item");
34: JMenu m1 = (JMenu) m.add(new HorizontalMenu("Submenu"));
35: m1.add("Submenu item");
36: m1.add("Submenu item");
37: }
38:
39: public static void main(String args[]) {
40: MenuLayoutDemo f = new MenuLayoutDemo();
41: f.pack();
42: f.setVisible(true);
43: }
44:
45: class HorizontalMenu extends JMenu {
46: HorizontalMenu(String label) {
47: super (label);
48: JPopupMenu pm = getPopupMenu();
49: pm.setLayout(new BoxLayout(pm, BoxLayout.X_AXIS));
50: setMinimumSize(getPreferredSize());
51: }
52:
53: public void setPopupMenuVisible(boolean b) {
54: boolean isVisible = isPopupMenuVisible();
55: if (b != isVisible) {
56: if ((b == true) && isShowing()) {
57: // Set location of popupMenu (pulldown or pullright)
58: // Perhaps this should be dictated by L&F
59: int x = 0;
60: int y = 0;
61: Container parent = getParent();
62: if (parent instanceof JPopupMenu) {
63: x = 0;
64: y = getHeight();
65: } else {
66: x = getWidth();
67: y = 0;
68: }
69: getPopupMenu().show(this , x, y);
70: } else {
71: getPopupMenu().setVisible(false);
72: }
73: }
74: }
75: }
76: }
|