Java Code Examples for java.awt.TrayIcon#setPopupMenu()

The following examples show how to use java.awt.TrayIcon#setPopupMenu() . 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: UpdatePopupMenu.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void createSystemTrayIcons() {

        final TrayIcon trayIcon = new TrayIcon(createSystemTrayIconImage());
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("Update Popup Menu items");

        try {
            trayIcon.setPopupMenu(createPopupMenu(trayIcon, 2));
            SystemTray.getSystemTray().add(trayIcon);

        } catch (AWTException ex) {
            throw new RuntimeException("System Tray cration failed");
        }
    }
 
Example 2
Source File: PopupMenuLeakTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void createSystemTrayIcon() {
    final TrayIcon trayIcon = new TrayIcon(createTrayIconImage());
    trayIcon.setImageAutoSize(true);

    try {
        // Add tray icon to system tray *before* adding popup menu to demonstrate buggy behaviour
        trayIcon.setPopupMenu(createTrayIconPopupMenu());
        SystemTray.getSystemTray().add(trayIcon);
        iconWeakReference.set(new WeakReference<>(trayIcon));
        popupWeakReference.set(new WeakReference<>(trayIcon.getPopupMenu()));
    } catch (final AWTException awte) {
        awte.printStackTrace();
    }
}
 
Example 3
Source File: JSystemTray.java    From PeerWasp with MIT License 5 votes vote down vote up
private TrayIcon create(Image image) throws IOException {
	TrayIcon trayIcon = new java.awt.TrayIcon(image);
	trayIcon.setImageAutoSize(true);
	trayIcon.setToolTip(tooltip);
	trayIcon.setPopupMenu(createMenu(false));
	return trayIcon;
}
 
Example 4
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 5
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 );
	}
}