Example usage for java.awt Menu Menu

List of usage examples for java.awt Menu Menu

Introduction

In this page you can find the example usage for java.awt Menu Menu.

Prototype

public Menu(String label) throws HeadlessException 

Source Link

Document

Constructs a new menu with the specified label.

Usage

From source file:PopupDemo.java

public PopupDemo() {
    MenuBar mb = new MenuBar();
    setMenuBar(mb);//from   ww w.j  av  a2 s  .  c  o  m
    Menu m = new Menu("file");
    mb.add(m);
    MenuItem item = new MenuItem("file-1");
    item.addActionListener(this);
    m.add(item);
    item = new MenuItem("file-2");
    m.add(item);

    setSize(100, 100);
    setLayout(new BorderLayout());

    Label l = new Label("label");
    addPopup(l, "label");
    add(l, "North");

    Panel p = new Panel();
    addPopup(p, "Panel");
    add(p, "Center");

    Button b = new Button("button");
    addPopup(b, "button");
    add(b, "South");
}

From source file:JDK6SplashTest.java

public JDK6SplashTest() {
    super("SplashScreen demo");
    setSize(500, 300);/*w ww. j a  v a  2 s.c o m*/
    setLayout(new BorderLayout());
    Menu m1 = new Menu("File");
    MenuItem mi1 = new MenuItem("Exit");
    m1.add(mi1);
    mi1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }

    });

    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    mb.add(m1);
    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
        System.out.println("SplashScreen.getSplashScreen() returned null");
        return;
    }
    Graphics2D g = (Graphics2D) splash.createGraphics();
    if (g == null) {
        System.out.println("g is null");
        return;
    }
    for (int i = 0; i < 100; i++) {
        renderSplashFrame(g, i);
        splash.update();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
        }
    }
    splash.close();
    setVisible(true);
    toFront();
}

From source file:ListenerReuse.java

public ListenerReuse() {
    Button b = new Button("Save");
    add(b);//from  w  w w . j a  va 2 s.  c o m
    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    Menu fm = new Menu("File");
    mb.add(fm);
    MenuItem mi = new MenuItem("Save");
    fm.add(mi);

    // Construct the ActionListener, and keep a reference to it.
    ActionListener saver = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("Saving your file...");
            // In real life we would call the doSave() method
            // in the main class, something like this:
            // mainProg.doSave();
        }
    };
    // Register the actionListener with the Button
    b.addActionListener(saver);
    // And now register the same actionListener with the MenuItem
    mi.addActionListener(saver);
    pack();
}

From source file:misc.TrayIconDemo.java

private static void createAndShowGUI() {
    //Check the SystemTray support
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*from   w w w  . ja  v a  2 s .  c  o  m*/
    }
    final PopupMenu popup = new PopupMenu();
    final TrayIcon trayIcon = new TrayIcon(createImage("images/bulb.gif", "tray icon"));
    final SystemTray tray = SystemTray.getSystemTray();

    // Create a popup menu components
    MenuItem aboutItem = new MenuItem("About");
    CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
    CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
    Menu displayMenu = new Menu("Display");
    MenuItem errorItem = new MenuItem("Error");
    MenuItem warningItem = new MenuItem("Warning");
    MenuItem infoItem = new MenuItem("Info");
    MenuItem noneItem = new MenuItem("None");
    MenuItem exitItem = new MenuItem("Exit");

    //Add components to popup menu
    popup.add(aboutItem);
    popup.addSeparator();
    popup.add(cb1);
    popup.add(cb2);
    popup.addSeparator();
    popup.add(displayMenu);
    displayMenu.add(errorItem);
    displayMenu.add(warningItem);
    displayMenu.add(infoItem);
    displayMenu.add(noneItem);
    popup.add(exitItem);

    trayIcon.setPopupMenu(popup);

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.out.println("TrayIcon could not be added.");
        return;
    }

    trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
        }
    });

    aboutItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "This dialog box is run from the About menu item");
        }
    });

    cb1.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb1Id = e.getStateChange();
            if (cb1Id == ItemEvent.SELECTED) {
                trayIcon.setImageAutoSize(true);
            } else {
                trayIcon.setImageAutoSize(false);
            }
        }
    });

    cb2.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            int cb2Id = e.getStateChange();
            if (cb2Id == ItemEvent.SELECTED) {
                trayIcon.setToolTip("Sun TrayIcon");
            } else {
                trayIcon.setToolTip(null);
            }
        }
    });

    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            MenuItem item = (MenuItem) e.getSource();
            //TrayIcon.MessageType type = null;
            System.out.println(item.getLabel());
            if ("Error".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.ERROR;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an error message",
                        TrayIcon.MessageType.ERROR);

            } else if ("Warning".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.WARNING;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is a warning message",
                        TrayIcon.MessageType.WARNING);

            } else if ("Info".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.INFO;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an info message",
                        TrayIcon.MessageType.INFO);

            } else if ("None".equals(item.getLabel())) {
                //type = TrayIcon.MessageType.NONE;
                trayIcon.displayMessage("Sun TrayIcon Demo", "This is an ordinary message",
                        TrayIcon.MessageType.NONE);
            }
        }
    };

    errorItem.addActionListener(listener);
    warningItem.addActionListener(listener);
    infoItem.addActionListener(listener);
    noneItem.addActionListener(listener);

    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            tray.remove(trayIcon);
            System.exit(0);
        }
    });
}

From source file:SplashDemo.java

public SplashDemo() {
    super("SplashScreen demo");
    setSize(300, 200);/*  www .  j  a  va  2s.c o  m*/
    setLayout(new BorderLayout());
    Menu m1 = new Menu("File");
    MenuItem mi1 = new MenuItem("Exit");
    m1.add(mi1);
    mi1.addActionListener(this);
    this.addWindowListener(closeWindow);

    MenuBar mb = new MenuBar();
    setMenuBar(mb);
    mb.add(m1);
    final SplashScreen splash = SplashScreen.getSplashScreen();
    if (splash == null) {
        System.out.println("SplashScreen.getSplashScreen() returned null");
        return;
    }
    Graphics2D g = splash.createGraphics();
    if (g == null) {
        System.out.println("g is null");
        return;
    }
    for (int i = 0; i < 100; i++) {
        renderSplashFrame(g, i);
        splash.update();
        try {
            Thread.sleep(90);
        } catch (InterruptedException e) {
        }
    }
    splash.close();
    setVisible(true);
    toFront();
}

From source file:AnotherMenuDemo.java

AnotherMenuDemo(String s) {
    super("MenuDemo: " + s);

    Container cp = this;
    cp.setLayout(new FlowLayout());

    mb = new MenuBar();
    setMenuBar(mb); // Frame implements MenuContainer

    MenuItem mi;// w  ww.  ja v a2  s . c o  m
    // The File Menu...
    fm = new Menu("File");
    fm.add(mi = new MenuItem("Open", new MenuShortcut('O')));
    mi.addActionListener(this);
    fm.add(mi = new MenuItem("Close", new MenuShortcut('W')));
    mi.addActionListener(this);
    fm.addSeparator();
    fm.add(mi = new MenuItem("Print", new MenuShortcut('P')));
    mi.addActionListener(this);
    fm.addSeparator();
    fm.add(mi = new MenuItem("Exit", new MenuShortcut('Q')));
    exitItem = mi; // save for action handler
    mi.addActionListener(this);
    mb.add(fm);

    // The Options Menu...
    om = new Menu("Options");
    cb = new CheckboxMenuItem("AutoSave");
    cb.setState(true);
    cb.addItemListener(this);
    om.add(cb);
    opSubm = new Menu("SubOptions");
    opSubm.add(new MenuItem("Alpha"));
    opSubm.add(new MenuItem("Gamma"));
    opSubm.add(new MenuItem("Delta"));
    om.add(opSubm);
    mb.add(om);

    // The Help Menu...
    hm = new Menu("Help");
    hm.add(mi = new MenuItem("About"));
    mi.addActionListener(this);
    hm.add(mi = new MenuItem("Topics"));
    mi.addActionListener(this);
    mb.add(hm);
    mb.setHelpMenu(hm); // needed for portability (Motif, etc.).

    // the main window
    cp.add(new Label("Menu Demo Window", 200, 150));
    pack();
}

From source file:PlayerOfMedia.java

/***************************************************************************
 * Construct a PlayerOfMedia. The Frame will have the title supplied by the
 * user. All initial actions on the PlayerOfMedia object are initiated
 * through its menu (or shotcut key)./*ww w . j a  v  a  2  s.co m*/
 **************************************************************************/
PlayerOfMedia(String name) {

    super(name);
    ///////////////////////////////////////////////////////////
    // Setup the menu system: a "File" menu with Open and Quit.
    ///////////////////////////////////////////////////////////
    bar = new MenuBar();
    fileMenu = new Menu("File");
    MenuItem openMI = new MenuItem("Open...", new MenuShortcut(KeyEvent.VK_O));
    openMI.setActionCommand("OPEN");
    openMI.addActionListener(this);
    fileMenu.add(openMI);
    MenuItem quitMI = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q));
    quitMI.addActionListener(this);
    quitMI.setActionCommand("QUIT");
    fileMenu.add(quitMI);
    bar.add(fileMenu);
    setMenuBar(bar);

    ///////////////////////////////////////////////////////
    // Layout the frame, its position on screen, and ensure
    // window closes are dealt with properly, including
    // relinquishing the resources of any Player.
    ///////////////////////////////////////////////////////
    setLayout(new BorderLayout());
    setLocation(100, 100);
    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            if (player != null) {
                player.stop();
                player.close();
            }
            System.exit(0);
        }
    });

    /////////////////////////////////////////////////////
    // Build the Dialog box by which the user can select
    // the media to play.
    /////////////////////////////////////////////////////
    selectionDialog = new Dialog(this, "Media Selection");
    Panel pan = new Panel();
    pan.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    mediaName = new TextField(40);
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    pan.add(mediaName, gbc);
    choose = new Button("Choose File...");
    gbc.ipadx = 10;
    gbc.ipady = 10;
    gbc.gridx = 2;
    gbc.gridwidth = 1;
    pan.add(choose, gbc);
    choose.addActionListener(this);
    open = new Button("Open");
    gbc.gridy = 1;
    gbc.gridx = 1;
    pan.add(open, gbc);
    open.addActionListener(this);
    cancel = new Button("Cancel");
    gbc.gridx = 2;
    pan.add(cancel, gbc);
    cancel.addActionListener(this);
    selectionDialog.add(pan);
    selectionDialog.pack();
    selectionDialog.setLocation(200, 200);

    ////////////////////////////////////////////////////
    // Build the error Dialog box by which the user can
    // be informed of any errors or problems.
    ////////////////////////////////////////////////////
    errorDialog = new Dialog(this, "Error", true);
    errorLabel = new Label("");
    errorDialog.add(errorLabel, "North");
    ok = new Button("OK");
    ok.addActionListener(this);
    errorDialog.add(ok, "South");
    errorDialog.pack();
    errorDialog.setLocation(150, 300);

    Manager.setHint(Manager.PLUGIN_PLAYER, new Boolean(true));
}

From source file:MDIApp.java

private MenuBar createMenuBar() {
    ActionListener al = new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            String command = ae.getActionCommand();
            if (command.equals("Open")) {
                if (fd == null) {
                    fd = new FileDialog(MDIApp.this, "Open File", FileDialog.LOAD);
                    fd.setDirectory("/movies");
                }//from   w w  w.  j av a  2  s.c o m
                fd.show();
                if (fd.getFile() != null) {
                    String filename = fd.getDirectory() + fd.getFile();
                    openFile("file:" + filename);
                }
            } else if (command.equals("Exit")) {
                dispose();
                System.exit(0);
            }
        }
    };

    MenuItem item;
    MenuBar mb = new MenuBar();
    // File Menu
    Menu mnFile = new Menu("File");
    mnFile.add(item = new MenuItem("Open"));
    item.addActionListener(al);
    mnFile.add(item = new MenuItem("Exit"));
    item.addActionListener(al);

    // Options Menu 
    Menu mnOptions = new Menu("Options");
    cbAutoLoop = new CheckboxMenuItem("Auto replay");
    cbAutoLoop.setState(true);
    mnOptions.add(cbAutoLoop);

    mb.add(mnFile);
    mb.add(mnOptions);
    return mb;
}

From source file:ExAmbientLight.java

public void initialize(String[] args) {
    // Initialize the window, menubar, etc.
    super.initialize(args);
    exampleFrame.setTitle("Java 3D Ambient Light Example");

    ///*from  w w w  .j av a 2 s  .  c o m*/
    //  Add a menubar menu to change node parameters
    //    Light on/off
    //    Color -->
    //

    Menu m = new Menu("AmbientLight");

    lightOnOffMenu = new CheckboxMenuItem("Light on/off", lightOnOff);
    lightOnOffMenu.addItemListener(this);
    m.add(lightOnOffMenu);

    colorMenu = new CheckboxMenu("Color", colors, currentColor, this);
    m.add(colorMenu);

    exampleMenuBar.add(m);
}

From source file:ExText.java

public void initialize(String[] args) {
    // Initialize the window, menubar, etc.
    super.initialize(args);
    exampleFrame.setTitle("Java 3D Text Example");

    ///*from   ww  w .  j a va2s. c  o m*/
    //  Add a menubar menu to change node parameters
    //    Font -->
    //

    Menu m = new Menu("Font3D");

    // Get a list of available fonts
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    String[] fontNames = ge.getAvailableFontFamilyNames();
    fonts = new NameValue[fontNames.length];
    Font newFont = null;

    // Add those fonts to a list and build the font menu
    if (debug)
        System.err.println("Loading fonts...");
    for (int i = 0; i < fontNames.length; i++) {
        if (debug)
            System.err.println("  " + fontNames[i]);
        newFont = new Font(fontNames[i], Font.PLAIN, 1);
        fonts[i] = new NameValue(fontNames[i], newFont);
    }
    fontMenu = new CheckboxMenu("Font", fonts, currentFont, this);
    m.add(fontMenu);

    exampleMenuBar.add(m);
}