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);
}
}
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();
}
}
Aggregations