I wrote java application that once configured starting to perform long time task and may stay without user interaction days, weeks, months, years.

For this kind of applications (good example mTorrent) very important to be possible close application window but keep application working and have possibility to open application from systray icon.

There are two code snippets below.

First creates systray icon with menu that have two menu items: "Open" and "Exit".

private void createTray() {

        Tray tray;

        TrayItem item;

        Image image;

        tray = display.getSystemTray();

        if (tray == null) {

            System.out.println("The system tray is not available");

        } else {

            item = new TrayItem(tray, SWT.NONE);

            item.setToolTipText("Wp Commenter");

            item.addListener(SWT.Show, new Listener() {

                public void handleEvent(Event event) {

                    System.out.println("show");

                }

            });

            item.addListener(SWT.Hide, new Listener() {

                public void handleEvent(Event event) {

                    System.out.println("hide");

                }

            });

            item.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    System.out.println("selection");

                }

            });

            item.addListener(SWT.DefaultSelection, new Listener() {

                public void handleEvent(Event event) {

                    System.out.println("default selection");

                }

            });

            final Menu menu = new Menu(shell, SWT.POP_UP);

 

            MenuItem openMenuItem = new MenuItem(menu, SWT.PUSH);

            openMenuItem.setText("Open");

            openMenuItem.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    shell.setVisible(true);

                    shell.setMaximized(true);

                }

            });

            MenuItem exitMenuItem = new MenuItem(menu, SWT.PUSH);

            exitMenuItem.setText("Exit");

            exitMenuItem.addListener(SWT.Selection, new Listener() {

                public void handleEvent(Event event) {

                    System.exit(0);

                }

            });

 

            item.addListener(SWT.MenuDetect, new Listener() {

                public void handleEvent(Event event) {

                    menu.setVisible(true);

                }

            });

 

            // image = SWTResourceManager.getImage(MakeBreak.class, "Backup-Green-Button-icon.png");

            image = SWTResourceManager.getImage(WpCommenter.class, "images/mb4.png");

            item.setImage(image);

        }

 

    }

Second adds listener to shell that "hide" application behind systray icon on "Closed" event.

shell.addShellListener(new ShellListener() {

            public void shellActivated(ShellEvent event) {

            }

 

            public void shellClosed(ShellEvent event) {

                event.doit = false;  //!! for this code i looked long time

                shell.setVisible(false);

            }

 

            public void shellDeactivated(ShellEvent event) {

            }

 

            public void shellDeiconified(ShellEvent event) {

            }

 

            public void shellIconified(ShellEvent event) {

                //shell.setVisible(false);

            }

        });

Filed under: java

Like this post? Subscribe to my RSS feed and get loads more!