Java – how to capture window minimization events?
•
Java
I want to create a JFrame instance and click the minimize button. I want to hide it in the system tray, usually the windows taskbar
I know through Java I can use the systemtray class in the AWT package, but I don't get any tutorials or working program examples
I ask this question here, either get a link to the tutorial site of systemtray class, or whether there is any text that knows how to capture window minimization events. This is a working example
Solution
This captures the window minimization event and creates a tray icon It will also remove the window from the taskbar, and it will add a listener to the tray icon so that a mouse click will restore the window The code is a bit cluttered, but it should be good enough for your learning purposes:
public class Qwe extends JFrame {
public static void main(String[] args) {
final Qwe qwe = new Qwe();
qwe.addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == ICONIFIED) {
try {
final TrayIcon trayIcon = new TrayIcon(new ImageIcon("/usr/share/icons/gnome/16x16/emotes/face-plain.png").getImage());
trayIcon.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
qwe.setVisible(true);
SystemTray.getSystemTray().remove(trayIcon);
}
});
SystemTray.getSystemTray().add(trayIcon);
qwe.setVisible(false);
} catch (AWTException e1) {
e1.printStackTrace();
}
}
}
});
qwe.setSize(200,200);
qwe.setVisible(true);
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
