Search in sources :

Example 6 with TrayIcon

use of java.awt.TrayIcon in project otapij by FellowTraveler.

the class MainPage method setToSystray.

private void setToSystray() {
    Image image = null;
    System.out.println("creating instance");
    if (SystemTray.isSupported()) {
        System.out.println("system tray supported");
        tray = SystemTray.getSystemTray();
        ImageIcon image1 = new javax.swing.ImageIcon(getClass().getResource("/com/moneychanger/ui/images/images.jpeg"));
        image = image1.getImage();
        ActionListener exitListener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Exiting....");
                System.exit(0);
            }
        };
        PopupMenu popup = new PopupMenu();
        MenuItem defaultItem = new MenuItem("Exit");
        defaultItem.addActionListener(exitListener);
        popup.add(defaultItem);
        defaultItem = new MenuItem("Open");
        defaultItem.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                //tray.remove(trayIcon);
                setVisible(true);
                repaint();
                setVisible(true);
                System.out.println("Open");
            }
        });
        popup.add(defaultItem);
        trayIcon = new TrayIcon(image, "Moneychanger", popup);
        trayIcon.setImageAutoSize(true);
    } else {
        System.out.println("system tray not supported");
    }
    final String os = System.getProperty("os.name").toLowerCase();
    addWindowStateListener(new WindowStateListener() {

        @Override
        public void windowStateChanged(WindowEvent e) {
            System.out.println("-----:" + e.getNewState());
            if (e.getNewState() == ICONIFIED) {
                try {
                    tray.add(trayIcon);
                    if (os.indexOf("nix") < 0 || os.indexOf("nux") < 0) {
                        setVisible(false);
                    }
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to tray");
                }
            }
            if (e.getNewState() == 7) {
                try {
                    tray.add(trayIcon);
                    if (!(os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0)) {
                        setVisible(false);
                    }
                    System.out.println("added to SystemTray");
                } catch (AWTException ex) {
                    System.out.println("unable to add to system tray");
                }
            }
            if (e.getNewState() == MAXIMIZED_BOTH) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Max both");
                System.out.println("Tray icon removed");
            }
            if (e.getNewState() == NORMAL) {
                tray.remove(trayIcon);
                setVisible(true);
                System.out.println("Max NORMAL");
                System.out.println("Tray icon removed");
            }
        }
    });
    //  setIconImage(Toolkit.getDefaultToolkit().getImage("Duke256.png"));
    if (image != null) {
        setIconImage(image);
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) WindowStateListener(java.awt.event.WindowStateListener) ActionEvent(java.awt.event.ActionEvent) MenuItem(java.awt.MenuItem) JMenuItem(javax.swing.JMenuItem) Image(java.awt.Image) ActionListener(java.awt.event.ActionListener) TrayIcon(java.awt.TrayIcon) WindowEvent(java.awt.event.WindowEvent) PopupMenu(java.awt.PopupMenu) JPopupMenu(javax.swing.JPopupMenu) AWTException(java.awt.AWTException)

Example 7 with TrayIcon

use of java.awt.TrayIcon in project jdk8u_jdk by JetBrains.

the class MostRecentKeyValue method dispose.

// Default to 1-second timeout for all
// interrupted Threads to exit, and another
// 1 second for all stopped Threads to die.
/**
     * Disposes of this AppContext, all of its top-level Frames, and
     * all Threads and ThreadGroups contained within it.
     *
     * This method must be called from a Thread which is not contained
     * within this AppContext.
     *
     * @exception  IllegalThreadStateException  if the current thread is
     *                                    contained within this AppContext
     * @since      1.2
     */
public void dispose() throws IllegalThreadStateException {
    // Check to be sure that the current Thread isn't in this AppContext
    if (this.threadGroup.parentOf(Thread.currentThread().getThreadGroup())) {
        throw new IllegalThreadStateException("Current Thread is contained within AppContext to be disposed.");
    }
    synchronized (this) {
        if (this.state != State.VALID) {
            // If already disposed or being disposed, bail.
            return;
        }
        this.state = State.BEING_DISPOSED;
    }
    final PropertyChangeSupport changeSupport = this.changeSupport;
    if (changeSupport != null) {
        changeSupport.firePropertyChange(DISPOSED_PROPERTY_NAME, false, true);
    }
    // First, we post an InvocationEvent to be run on the
    // EventDispatchThread which disposes of all top-level Frames and TrayIcons
    final Object notificationLock = new Object();
    Runnable runnable = new Runnable() {

        public void run() {
            Window[] windowsToDispose = Window.getOwnerlessWindows();
            for (Window w : windowsToDispose) {
                try {
                    w.dispose();
                } catch (Throwable t) {
                    log.finer("exception occurred while disposing app context", t);
                }
            }
            AccessController.doPrivileged(new PrivilegedAction<Void>() {

                public Void run() {
                    if (!GraphicsEnvironment.isHeadless() && SystemTray.isSupported()) {
                        SystemTray systemTray = SystemTray.getSystemTray();
                        TrayIcon[] trayIconsToDispose = systemTray.getTrayIcons();
                        for (TrayIcon ti : trayIconsToDispose) {
                            systemTray.remove(ti);
                        }
                    }
                    return null;
                }
            });
            // Alert PropertyChangeListeners that the GUI has been disposed.
            if (changeSupport != null) {
                changeSupport.firePropertyChange(GUI_DISPOSED, false, true);
            }
            synchronized (notificationLock) {
                // Notify caller that we're done
                notificationLock.notifyAll();
            }
        }
    };
    synchronized (notificationLock) {
        SunToolkit.postEvent(this, new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
        try {
            notificationLock.wait(DISPOSAL_TIMEOUT);
        } catch (InterruptedException e) {
        }
    }
    // Next, we post another InvocationEvent to the end of the
    // EventQueue.  When it's executed, we know we've executed all
    // events in the queue.
    runnable = new Runnable() {

        public void run() {
            synchronized (notificationLock) {
                // Notify caller that we're done
                notificationLock.notifyAll();
            }
        }
    };
    synchronized (notificationLock) {
        SunToolkit.postEvent(this, new InvocationEvent(Toolkit.getDefaultToolkit(), runnable));
        try {
            notificationLock.wait(DISPOSAL_TIMEOUT);
        } catch (InterruptedException e) {
        }
    }
    // We are done with posting events, so change the state to disposed
    synchronized (this) {
        this.state = State.DISPOSED;
    }
    // Next, we interrupt all Threads in the ThreadGroup
    this.threadGroup.interrupt();
    // Note, the EventDispatchThread we've interrupted may dump an
    // InterruptedException to the console here.  This needs to be
    // fixed in the EventDispatchThread, not here.
    // Next, we sleep 10ms at a time, waiting for all of the active
    // Threads in the ThreadGroup to exit.
    long startTime = System.currentTimeMillis();
    long endTime = startTime + THREAD_INTERRUPT_TIMEOUT;
    while ((this.threadGroup.activeCount() > 0) && (System.currentTimeMillis() < endTime)) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
    }
    // Then, we stop any remaining Threads
    this.threadGroup.stop();
    // Next, we sleep 10ms at a time, waiting for all of the active
    // Threads in the ThreadGroup to die.
    startTime = System.currentTimeMillis();
    endTime = startTime + THREAD_INTERRUPT_TIMEOUT;
    while ((this.threadGroup.activeCount() > 0) && (System.currentTimeMillis() < endTime)) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
        }
    }
    // Next, we remove this and all subThreadGroups from threadGroup2appContext
    int numSubGroups = this.threadGroup.activeGroupCount();
    if (numSubGroups > 0) {
        ThreadGroup[] subGroups = new ThreadGroup[numSubGroups];
        numSubGroups = this.threadGroup.enumerate(subGroups);
        for (int subGroup = 0; subGroup < numSubGroups; subGroup++) {
            threadGroup2appContext.remove(subGroups[subGroup]);
        }
    }
    threadGroup2appContext.remove(this.threadGroup);
    threadAppContext.set(null);
    // Finally, we destroy the ThreadGroup entirely.
    try {
        this.threadGroup.destroy();
    } catch (IllegalThreadStateException e) {
    // Fired if not all the Threads died, ignore it and proceed
    }
    synchronized (table) {
        // Clear out the Hashtable to ease garbage collection
        this.table.clear();
    }
    numAppContexts.decrementAndGet();
    mostRecentKeyValue = null;
}
Also used : Window(java.awt.Window) SystemTray(java.awt.SystemTray) PropertyChangeSupport(java.beans.PropertyChangeSupport) InvocationEvent(java.awt.event.InvocationEvent) TrayIcon(java.awt.TrayIcon)

Example 8 with TrayIcon

use of java.awt.TrayIcon in project jdk8u_jdk by JetBrains.

the class MultiResolutionTrayIconTest method createUI.

public static void createUI() throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {

        public void run() {
            mainFrame = new JFrame("TrayIcon Test");
            boolean trayIsSupported = SystemTray.isSupported();
            tray = SystemTray.getSystemTray();
            Dimension d = tray.getTrayIconSize();
            icon = new TrayIcon(createIcon(d.width, d.height));
            icon.setImageAutoSize(true);
            layout = new GridBagLayout();
            mainControlPanel = new JPanel(layout);
            resultButtonPanel = new JPanel(layout);
            GridBagConstraints gbc = new GridBagConstraints();
            String instructions = "<html>INSTRUCTIONS:<br>" + "Press start button to add icon to system tray.<br><br>" + "If Icon color is green test" + " passes else failed.<br><br></html>";
            instructionText = new JLabel();
            instructionText.setText(instructions);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            mainControlPanel.add(instructionText, gbc);
            startButton = new JButton("Start");
            startButton.setActionCommand("Start");
            if (trayIsSupported) {
                startButton.addActionListener((ActionEvent e) -> {
                    doTest();
                });
            } else {
                startButton.setEnabled(false);
                System.out.println("system tray is not supported");
                latch.countDown();
            }
            gbc.gridx = 0;
            gbc.gridy = 0;
            resultButtonPanel.add(startButton, gbc);
            passButton = new JButton("Pass");
            passButton.setActionCommand("Pass");
            passButton.addActionListener((ActionEvent e) -> {
                latch.countDown();
                removeIcon();
                mainFrame.dispose();
            });
            failButton = new JButton("Fail");
            failButton.setActionCommand("Fail");
            failButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    removeIcon();
                    latch.countDown();
                    mainFrame.dispose();
                    throw new RuntimeException("Test Failed");
                }
            });
            gbc.gridx = 1;
            gbc.gridy = 0;
            resultButtonPanel.add(passButton, gbc);
            gbc.gridx = 2;
            gbc.gridy = 0;
            resultButtonPanel.add(failButton, gbc);
            gbc.gridx = 0;
            gbc.gridy = 1;
            mainControlPanel.add(resultButtonPanel, gbc);
            mainFrame.add(mainControlPanel);
            mainFrame.setSize(400, 200);
            mainFrame.setLocationRelativeTo(null);
            mainFrame.setVisible(true);
            mainFrame.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosing(WindowEvent e) {
                    removeIcon();
                    latch.countDown();
                    mainFrame.dispose();
                }
            });
        }
    });
}
Also used : JPanel(javax.swing.JPanel) GridBagConstraints(java.awt.GridBagConstraints) GridBagLayout(java.awt.GridBagLayout) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) WindowAdapter(java.awt.event.WindowAdapter) Dimension(java.awt.Dimension) ActionListener(java.awt.event.ActionListener) JFrame(javax.swing.JFrame) TrayIcon(java.awt.TrayIcon) WindowEvent(java.awt.event.WindowEvent)

Example 9 with TrayIcon

use of java.awt.TrayIcon in project processdash by dtuma.

the class SystemTrayIconJDK6Impl method initialize.

/**
     * {@inheritDoc}
     */
public void initialize(ProcessDashboard pdash) {
    if (this.pdash != null || this.icon != null) {
        logger.warning("SystemTrayIconJDK6Impl already initialized");
        return;
    }
    this.icon = new TrayIcon(getImage());
    this.isVisible = false;
    this.pdash = pdash;
    reminder = new Reminder(icon, pdash);
    imageHandler = new IconImageHandler(pdash, icon);
    tooltipHandler = new TooltipHandler(pdash, icon);
    windowHandler = new WindowHandler(pdash, this);
    menuHandler = new MenuHandler(pdash, icon, reminder);
    mouseHandler = new MouseHandler(pdash, icon, menuHandler, imageHandler);
    userSettingHandler = new UserSettingHandler(this);
}
Also used : TrayIcon(java.awt.TrayIcon)

Aggregations

TrayIcon (java.awt.TrayIcon)9 ActionEvent (java.awt.event.ActionEvent)5 Image (java.awt.Image)4 MenuItem (java.awt.MenuItem)4 PopupMenu (java.awt.PopupMenu)4 ActionListener (java.awt.event.ActionListener)4 AWTException (java.awt.AWTException)3 Dimension (java.awt.Dimension)2 SystemTray (java.awt.SystemTray)2 WindowEvent (java.awt.event.WindowEvent)2 IOException (java.io.IOException)2 ImageIcon (javax.swing.ImageIcon)2 JButton (javax.swing.JButton)2 JFrame (javax.swing.JFrame)2 HTTPRequests (APJP.HTTP11.HTTPRequests)1 HTTPSRequests (APJP.HTTP11.HTTPSRequests)1 ProxyServer (APJP.ProxyServer)1 IAIK (iaik.security.provider.IAIK)1 BorderLayout (java.awt.BorderLayout)1 Color (java.awt.Color)1