Java Code Examples for java.awt.MenuItem#addActionListener()

The following examples show how to use java.awt.MenuItem#addActionListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StaveActionHandler.java    From jmg with GNU General Public License v2.0 6 votes vote down vote up
StaveActionHandler(Stave stave) {
theApp = stave;

      noteContextMenu = new PopupMenu();
      
      editNote = new MenuItem("Edit Note");
      editNote.addActionListener(this);
      noteContextMenu.add(editNote );       
      
      repeatNote = new MenuItem("Repeat Note");
      repeatNote.addActionListener(this);
      noteContextMenu.add(repeatNote ); 		
      
      makeRest = new MenuItem("Change to Rest");
      makeRest.addActionListener(this);
      noteContextMenu.add(makeRest);       
      
      deleteNote = new MenuItem("Delete Note");
      deleteNote.addActionListener(this);
      noteContextMenu.add(deleteNote );       
      
      theApp.add(noteContextMenu);
  }
 
Example 2
Source File: UpdatePopupMenu.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private PopupMenu createPopupMenu(final TrayIcon trayIcon,
        final int menuCount) {

    final PopupMenu trayIconPopupMenu = new PopupMenu();

    for (int i = 1; i <= menuCount; ++i) {
        final MenuItem popupMenuItem = new MenuItem("MenuItem_" + i);

        popupMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent ae) {
                trayIcon.setPopupMenu(createPopupMenu(trayIcon,
                        menuCount + 1));
            }
        });

        trayIconPopupMenu.add(popupMenuItem);
    }

    return trayIconPopupMenu;
}
 
Example 3
Source File: DatabaseManager.java    From evosql with Apache License 2.0 6 votes vote down vote up
private void addToRecent(String s) {

        for (int i = 0; i < iMaxRecent; i++) {
            if (s.equals(sRecent[i])) {
                return;
            }
        }

        if (sRecent[iRecent] != null) {
            mRecent.remove(iRecent);
        }

        sRecent[iRecent] = s;

        if (s.length() > 43) {
            s = s.substring(0, 40) + "...";
        }

        MenuItem item = new MenuItem(s);

        item.setActionCommand("#" + iRecent);
        item.addActionListener(this);
        mRecent.insert(item, iRecent);

        iRecent = (iRecent + 1) % iMaxRecent;
    }
 
Example 4
Source File: Manager.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private MenuItem createPreferences() {
    MenuItem pItem = new MenuItem(getString("Action.Preferences"));
    pItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setup();
        }
    });
    return pItem;
}
 
Example 5
Source File: Manager.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private MenuItem createExit() {
    MenuItem exitItem = new MenuItem(GlobalResourcesManager
            .getString("Action.Exit"));
    exitItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            exit();
        }
    });
    return exitItem;
}
 
Example 6
Source File: Boot.java    From MakeLobbiesGreatAgain with MIT License 5 votes vote down vote up
public static void setupTray() throws AWTException {
	final SystemTray tray = SystemTray.getSystemTray();
	final PopupMenu popup = new PopupMenu();
	final MenuItem info = new MenuItem();
	final MenuItem exit = new MenuItem();
	final TrayIcon trayIcon = new TrayIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB), "MLGA", popup);
	try {
		InputStream is = FileUtil.localResource("icon.png");
		trayIcon.setImage(ImageIO.read(is));
		is.close();
	} catch (IOException e1) {
		e1.printStackTrace();
	}

	info.addActionListener(e -> {
		String message = "Double-Click to lock/unlock the overlay for dragging";
		JOptionPane.showMessageDialog(null, message, "Information", JOptionPane.INFORMATION_MESSAGE);
	});

	exit.addActionListener(e -> {
		running = false;
		tray.remove(trayIcon);
		ui.close();
		System.out.println("Terminated UI...");
		System.out.println("Cleaning up system resources. Could take a while...");
		handle.close();
		System.out.println("Killed handle.");
		System.exit(0);
	});
	info.setLabel("Help");
	exit.setLabel("Exit");
	popup.add(info);
	popup.add(exit);
	tray.add(trayIcon);
}
 
Example 7
Source File: Manager.java    From ramus with GNU General Public License v3.0 5 votes vote down vote up
private MenuItem createStop() {
    stopItem = new MenuItem(getString("Action.Stop"));
    stopItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            stop();
        }
    });
    stopItem.setEnabled(false);
    return stopItem;
}
 
Example 8
Source File: ActionEventTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public ActionEventTest() {
    menuBar = new MenuBar();
    Menu menu = new Menu("Menu1");
    MenuItem menuItem = new MenuItem("MenuItem");

    menuItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            System.out.println("actionPerformed");
            int md = ae.getModifiers();
            int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
                    | ActionEvent.SHIFT_MASK;

            isProgInterruption = true;
            mainThread.interrupt();
            if ((md & expectedMask) != expectedMask) {
                throw new RuntimeException("Action Event modifiers are not"
                    + " set correctly.");
            }
        }
    });
    menu.add(menuItem);
    menuBar.add(menu);
    setMenuBar(menuBar);

    instructions = new TextArea(10, 50);
    instructions.setText(
    " This is a manual test\n" +
    " Keep the Alt, Shift & Ctrl Keys pressed while doing next steps\n" +
    " Click 'Menu1' Menu from the Menu Bar\n" +
    " It will show 'MenuItem'\n" +
    " Left mouse Click the 'MenuItem'\n" +
    " Test exits automatically after mouse click.");
    add(instructions);

    setSize(400, 400);
    setVisible(true);
    validate();
}
 
Example 9
Source File: DatabaseManager.java    From evosql with Apache License 2.0 5 votes vote down vote up
void addMenuItems(Menu f, String[] m) {

        for (int i = 0; i < m.length; i++) {
            MenuItem item = new MenuItem(m[i].substring(1));
            char     c    = m[i].charAt(0);

            if (c != '-') {
                item.setShortcut(new MenuShortcut(c));
            }

            item.addActionListener(this);
            f.add(item);
        }
    }
 
Example 10
Source File: Transfer.java    From evosql with Apache License 2.0 5 votes vote down vote up
/**
 * Method declaration
 *
 *
 * @param f
 * @param m
 */
private void addMenuItems(Menu f, String[] m) {

    for (int i = 0; i < m.length; i++) {
        if (m[i].equals("-")) {
            f.addSeparator();
        } else {
            MenuItem item = new MenuItem(m[i]);

            item.addActionListener(this);
            f.add(item);
        }
    }
}
 
Example 11
Source File: JTrayMenu.java    From PeerWasp with MIT License 4 votes vote down vote up
private MenuItem createSettingsMenu() {
	MenuItem settingsItem = new MenuItem("Settings");
	settingsItem.addActionListener(createSettingsListener());
	return settingsItem;
}
 
Example 12
Source File: GUI.java    From CQL with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings({})
	public static Pair<JPanel, MenuBar> makeGUI(JFrame frame) {
		topFrame = frame;

		MenuBar menuBar = new MenuBar();

		Menu fileMenu = makeFileMenu();

		Menu editMenu = makeEditMenu();

		Menu toolsMenu = makesToolsMenu();

		Menu helpMenu = new Menu("Help");
		MenuItem aboutItem = new MenuItem("Help");
		helpMenu.add(aboutItem);
		aboutItem.addActionListener(e -> IdeOptions.showAbout());

		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(toolsMenu);

		Menu aqlMenu = populateAql();

		menuBar.add(aqlMenu);

		//Menu legacyMenu = new Menu("Legacy");
		//Menu fqlMenu = new Menu("FQL");
		//Menu fqlppMenu = new Menu("FQL++");
//		Menu oplMenu = new Menu("OPL");
//		Menu fpqlMenu = new Menu("FPQL");
		//legacyMenu.add(fqlMenu);
		//legacyMenu.add(fqlppMenu);
//		legacyMenu.add(oplMenu);
//		legacyMenu.add(fpqlMenu);
		//MenuItem optionsItem2 = new MenuItem("Legacy options");
		//legacyMenu.add(optionsItem2);
		//optionsItem2.addActionListener(e -> DefunctGlobalOptions.showOptions());

		// menuBar.add(legacyMenu);

		menuBar.add(helpMenu);

		JPanel pan = new JPanel();

		pan.setLayout(new BorderLayout());

		JPanel toolBar = makeToolBar();

		pan.add(toolBar, BorderLayout.PAGE_START);
		pan.add(editors, BorderLayout.CENTER);

		return new Pair<>(pan, menuBar);
	}
 
Example 13
Source File: JTrayMenu.java    From PeerWasp with MIT License 4 votes vote down vote up
private MenuItem createRootFolderMenu() {
	MenuItem rootItem = new MenuItem("Open Folder");
	rootItem.addActionListener(createRootFolderListener());
	return rootItem;
}
 
Example 14
Source File: TrayController.java    From subsonic with GNU General Public License v3.0 4 votes vote down vote up
private MenuItem createMenuItem(Action action) {
    MenuItem menuItem = new MenuItem((String) action.getValue(Action.NAME));
    menuItem.addActionListener(action);
    return menuItem;
}
 
Example 15
Source File: MainFrame.java    From scelight with Apache License 2.0 4 votes vote down vote up
/**
 * Installs a system tray icon.
 */
private void installTrayIcon() {
	if ( !SystemTray.isSupported() )
		return;
	
	final TrayIcon trayIcon = new TrayIcon( Icons.MY_APP_ICON.get().getImage(), Consts.APP_NAME_FULL + " is running." );
	trayIcon.setImageAutoSize( true );
	
	try {
		SystemTray.getSystemTray().add( trayIcon );
		
		this.trayIcon = trayIcon;
		
		trayIcon.addActionListener( Actions.SHOW_MAIN_FRAME );
		
		final PopupMenu popup = new PopupMenu();
		final MenuItem restoreMenuItem = new MenuItem( "Show Main Window" );
		restoreMenuItem.addActionListener( Actions.SHOW_MAIN_FRAME );
		popup.add( restoreMenuItem );
		final MenuItem hideMenuItem = new MenuItem( "Hide Main Window" );
		hideMenuItem.addActionListener( Actions.MINIMIZE_TO_TRAY );
		popup.add( hideMenuItem );
		popup.addSeparator();
		final MenuItem restoreDefPosMenuItem = new MenuItem( "Restore Main Window to defaults" );
		restoreDefPosMenuItem.addActionListener( new ActionAdapter() {
			@Override
			public void actionPerformed( final ActionEvent e ) {
				// First ensure it's visible and active:
				Actions.SHOW_MAIN_FRAME.actionPerformed( null );
				// And the default position:
				Actions.RESTORE_DEF_WIN_POSITION.actionPerformed( null );
			}
		} );
		popup.add( restoreDefPosMenuItem );
		popup.addSeparator();
		final MenuItem exitMenuItem = new MenuItem( "Exit" );
		exitMenuItem.addActionListener( Actions.EXIT );
		popup.add( exitMenuItem );
		trayIcon.setPopupMenu( popup );
		
		Actions.MINIMIZE_TO_TRAY.setEnabled( true );
	} catch ( final AWTException ae ) {
		Env.LOGGER.debug( "Failed to install tray icon!", ae );
	}
}
 
Example 16
Source File: JTrayMenu.java    From PeerWasp with MIT License 4 votes vote down vote up
private MenuItem createActivityMenu() {
	MenuItem activityItem = new MenuItem("Activity");
	activityItem.addActionListener(createActivityListener());
	return activityItem;
}
 
Example 17
Source File: JTrayMenu.java    From PeerWasp with MIT License 4 votes vote down vote up
private MenuItem createQuitMenu() {
	MenuItem closeItem = new MenuItem("Quit");
	closeItem.addActionListener(createQuitListener());
	return closeItem;
}
 
Example 18
Source File: POELevelFx.java    From Path-of-Leveling with MIT License 4 votes vote down vote up
private void addTrayIcon() throws AWTException {
    final TrayIcon trayIcon = new TrayIcon(new ImageIcon(getClass().getResource("/icons/The_Explorer_card_art.png")).getImage(), "Path of Leveling");

    // Create a pop-up menu components
    final PopupMenu popup = new PopupMenu();
    final MenuItem shutdownItem = new MenuItem("Exit");
    final MenuItem settingsItem = new MenuItem("Settings");

    //Add components to pop-up menu
    popup.add(settingsItem);
    popup.add(shutdownItem);

    trayIcon.setPopupMenu(popup);
    trayIcon.setImageAutoSize(true); //So the icon auto-sizes

    SystemTray.getSystemTray().add(trayIcon);

    trayIcon.addMouseListener(new MouseAdapter() {
        @Override public void mouseClicked(java.awt.event.MouseEvent e) {
            if (e.getButton() == java.awt.event.MouseEvent.BUTTON1) { //Left click on tray icon (single click !)
                Platform.runLater(() -> {
                    //code to show things...
                });
            }
        }
    });

    shutdownItem.addActionListener(evnt -> {
        //code to exit the program
        //save stuff
        if(saveBuildsToMemory()){
            System.out.println("Successfully saved checkpoint");
        }else{
            System.out.println("Checkpoint save failed");
        }
        try {
                GlobalScreen.unregisterNativeHook();
        } catch (NativeHookException e1) {
                e1.printStackTrace();
        }
        System.exit(20);
    });

    settingsItem.addActionListener(evnt -> {
        //code to exit the program
        //save stuff
        if(controller!=null){
            controller.settings_event();
        }
    });
}
 
Example 19
Source File: AppleDock.java    From Spark with Apache License 2.0 4 votes vote down vote up
public AppleDock() {

	PopupMenu menu = new PopupMenu();

	PopupMenu statusmenu = new PopupMenu(Res.getString("menuitem.status"));

	for (Presence p : PresenceManager.getPresences()) {
	    MenuItem dd = new MenuItem(p.getStatus());
	    dd.addActionListener(this);
	    statusmenu.add(dd);
	}

	menu.add(statusmenu);

	JFrame frame = SparkManager.getMainWindow();
	frame.add(menu);

	// set dock menu
	Application app = new Application();
	app.setDockMenu(menu);

    }
 
Example 20
Source File: GUI.java    From CQL with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
	private static Menu makeFileMenu() {
		Menu fileMenu = new Menu("File");

		MenuItem openItem = new MenuItem("Open");
		MenuItem saveItem = new MenuItem("Save");
		MenuItem saveAsItem = new MenuItem("Save As");
		MenuItem saveAllItem = new MenuItem("Save All");
		MenuItem closeItem = new MenuItem("Close");
		MenuItem exitItem = new MenuItem("Quit");

		Map<Language, MenuItem> newItems = new THashMap<>();
		for (Language l : Language.values0()) {
			if (l.toString().contains(" ")) {
				continue;
			}
			if (l.toString().contains("EASIK")) {
				continue;
			}
			MenuItem newItem = new MenuItem("New " + l);
			fileMenu.add(newItem);
			newItems.put(l, newItem);
			newItem.addActionListener((ActionEvent e) -> newAction(null, "", l));
		}

		fileMenu.add(openItem);
		// fileMenu.add(openItem2);
		fileMenu.add(saveItem);
		fileMenu.add(saveAsItem);
		fileMenu.add(saveAllItem);
		fileMenu.add(closeItem);
		fileMenu.add(exitItem);

		closeItem.addActionListener(e -> closeAction());
		saveAsItem.addActionListener(e -> saveAsActionAlternate(getSelectedEditor()));

		KeyStroke ctrlS = KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut s = new MenuShortcut(ctrlS.getKeyCode());
		saveItem.setShortcut(s);

		// TODO aql why doesn't shift mask work? order?
		MenuShortcut sA = new MenuShortcut(ctrlS.getKeyCode(), true);
		saveAllItem.setShortcut(sA);
		saveAllItem.addActionListener(e -> saveAllAction());

		KeyStroke ctrlQ = KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut q = new MenuShortcut(ctrlQ.getKeyCode());
		exitItem.setShortcut(q);

		KeyStroke ctrlW = KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
//		KeyStroke commandW = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_DOWN_MASK);
		MenuShortcut c = new MenuShortcut(ctrlW.getKeyCode());
		closeItem.setShortcut(c);

		openItem.addActionListener(e -> openActionAlternate());

		saveItem.addActionListener(e -> saveAction());

		exitItem.addActionListener(e -> exitAction());

		KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut n = new MenuShortcut(ctrlN.getKeyCode());
		newItems.get(Language.getDefault()).setShortcut(n);
		KeyStroke ctrlO = KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut o = new MenuShortcut(ctrlO.getKeyCode());
		openItem.setShortcut(o);
		return fileMenu;

	}