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

The following examples show how to use java.awt.TrayIcon#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: DownApplication.java    From proxyee-down with Apache License 2.0 6 votes vote down vote up
private void initTray() throws AWTException {
  if (SystemTray.isSupported()) {
    // 获得系统托盘对象
    SystemTray systemTray = SystemTray.getSystemTray();
    // 获取图片所在的URL
    URL url = Thread.currentThread().getContextClassLoader().getResource(ICON_PATH);
    // 为系统托盘加托盘图标
    Image trayImage = Toolkit.getDefaultToolkit().getImage(url);
    Dimension trayIconSize = systemTray.getTrayIconSize();
    trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);
    trayIcon = new TrayIcon(trayImage, "Proxyee Down");
    systemTray.add(trayIcon);
    loadPopupMenu();
    //双击事件监听
    trayIcon.addActionListener(event -> Platform.runLater(() -> loadUri(null, true)));
  }
}
 
Example 2
Source File: ActionEventTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private void initializeGUI() {

        icon = new TrayIcon(
            new BufferedImage(20, 20, BufferedImage.TYPE_INT_RGB), "ti");
        icon.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                actionPerformed = true;
                int md = ae.getModifiers();
                int expectedMask = ActionEvent.ALT_MASK | ActionEvent.CTRL_MASK
                        | ActionEvent.SHIFT_MASK;

                if ((md & expectedMask) != expectedMask) {
                    clear();
                    throw new RuntimeException("Action Event modifiers are not"
                        + " set correctly.");
                }
            }
        });

        try {
            SystemTray.getSystemTray().add(icon);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
Example 3
Source File: SysTray.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private TrayIcon createTrayIcon() {
    Image image = createTrayImage();
    String tooltip = createTrayTooltip();
    trayPopup = createTrayPopup();
    TrayIcon icon = new TrayIcon(image, tooltip, trayPopup);
    icon.setImageAutoSize(true);

    icon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    if (trayPopup.isEnabled()) toggleWindowVisibility();
                }
            });
        }
    });

    return icon;
}
 
Example 4
Source File: NeembuuUploader.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
private void setUpTrayIcon() {
    if (SystemTray.isSupported()) {
        //trayIcon.setImageAutoSize(true); It renders the icon very poorly.
        //So we render the icon ourselves with smooth settings.
        {
            Dimension d = SystemTray.getSystemTray().getTrayIconSize();
            trayIcon = new TrayIcon(getIconImage().getScaledInstance(d.width, d.height, Image.SCALE_SMOOTH));
        }
        //trayIcon = new TrayIcon(getIconImage());
        //trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip(Translation.T().trayIconToolTip());
        trayIcon.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                NULogger.getLogger().info("System tray double clicked");

                setExtendedState(JFrame.NORMAL);
                setVisible(true);
                repaint();
                SystemTray.getSystemTray().remove(trayIcon);
            }
        });
    }
}
 
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 );
	}
}