Search in sources :

Example 1 with WindowStateListener

use of java.awt.event.WindowStateListener 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 2 with WindowStateListener

use of java.awt.event.WindowStateListener in project jdk8u_jdk by JetBrains.

the class NormalToIconifiedTest method main.

public static void main(String[] args) {
    Robot robot = Util.createRobot();
    Frame testFrame = new Frame("Test Frame");
    testFrame.setSize(200, 200);
    testFrame.addWindowStateListener(new WindowStateListener() {

        @Override
        public void windowStateChanged(WindowEvent e) {
            listenerNotified.set(true);
            synchronized (listenerNotified) {
                listenerNotified.notifyAll();
            }
        }
    });
    testFrame.setVisible(true);
    Frame mainFrame = new Frame("Main Frame");
    mainFrame.setSize(200, 200);
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
    Util.waitForIdle(robot);
    try {
        Util.clickOnComp(mainFrame, robot);
        Util.waitForIdle(robot);
        // NORMAL -> ICONIFIED
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.ICONIFIED);
        Util.waitForIdle(robot);
        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during NORMAL to" + "ICONIFIED transition");
        }
        if (testFrame.getExtendedState() != Frame.ICONIFIED) {
            throw new RuntimeException("Test FAILED! Frame is not in ICONIFIED state");
        }
        // ICONIFIED -> NORMAL
        listenerNotified.set(false);
        testFrame.setExtendedState(Frame.NORMAL);
        Util.waitForIdle(robot);
        Util.waitForCondition(listenerNotified, 2000);
        if (!listenerNotified.get()) {
            throw new RuntimeException("Test FAILED! Window state listener was not notified during ICONIFIED to" + "NORMAL transition");
        }
        if (testFrame.getExtendedState() != Frame.NORMAL) {
            throw new RuntimeException("Test FAILED! Frame is not in NORMAL state");
        }
    } finally {
        testFrame.dispose();
        mainFrame.dispose();
    }
}
Also used : Frame(java.awt.Frame) WindowStateListener(java.awt.event.WindowStateListener) WindowEvent(java.awt.event.WindowEvent) Robot(java.awt.Robot)

Aggregations

WindowEvent (java.awt.event.WindowEvent)2 WindowStateListener (java.awt.event.WindowStateListener)2 AWTException (java.awt.AWTException)1 Frame (java.awt.Frame)1 Image (java.awt.Image)1 MenuItem (java.awt.MenuItem)1 PopupMenu (java.awt.PopupMenu)1 Robot (java.awt.Robot)1 TrayIcon (java.awt.TrayIcon)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ImageIcon (javax.swing.ImageIcon)1 JMenuItem (javax.swing.JMenuItem)1 JPopupMenu (javax.swing.JPopupMenu)1