Open popup(Menu) on task tray icon with left click using java

前端 未结 5 1195
难免孤独
难免孤独 2020-12-20 13:13

I am working on task tray Icon in java, I like to open a popup Menu using left click same popup Menu as I open on right click, and please help me with a quick response.

相关标签:
5条回答
  • 2020-12-20 13:39

    What you actually lack is a parent component to show your PopupMenu. One way to achieve this, is to use an "invisible" frame (actually it is visible but with 0-bounds and undecorated, so you can't see it) like this:

    import java.awt.AWTException;
    import java.awt.CheckboxMenuItem;
    import java.awt.Frame;
    import java.awt.Menu;
    import java.awt.MenuItem;
    import java.awt.PopupMenu;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    public class MyTaskTray {
        public static void main(String arg[]) throws MalformedURLException {
            final Frame frame = new Frame("");
            frame.setUndecorated(true);
            // Check the SystemTray is supported
            if (!SystemTray.isSupported()) {
                System.out.println("SystemTray is not supported");
                return;
            }
            final TrayIcon trayIcon = new TrayIcon(Toolkit.getDefaultToolkit().getImage(
                    new URL("http://home.comcast.net/~supportcd/Icons/Java_Required.jpg")), "Library Drop");
            final SystemTray tray = SystemTray.getSystemTray();
    
            // Create a pop-up menu components
            final PopupMenu popup = createPopupMenu();
            trayIcon.setPopupMenu(popup);
            trayIcon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getButton() == MouseEvent.BUTTON1) {
                        frame.add(popup);
                        popup.show(frame, e.getXOnScreen(), e.getYOnScreen());
                    }
                }
            });
            try {
                frame.setResizable(false);
                frame.setVisible(true);
                tray.add(trayIcon);
            } catch (AWTException e) {
                System.out.println("TrayIcon could not be added.");
            }
    
        }
    
        protected static PopupMenu createPopupMenu() {
            final PopupMenu popup = new PopupMenu();
            MenuItem aboutItem = new MenuItem("About");
            CheckboxMenuItem cb1 = new CheckboxMenuItem("Set auto size");
            CheckboxMenuItem cb2 = new CheckboxMenuItem("Set tooltip");
            Menu displayMenu = new Menu("Display");
            MenuItem errorItem = new MenuItem("Error");
            MenuItem warningItem = new MenuItem("Warning");
            MenuItem infoItem = new MenuItem("Info");
            MenuItem noneItem = new MenuItem("None");
            MenuItem exitItem = new MenuItem("Exit");
            // Add components to pop-up menu
            popup.add(aboutItem);
            popup.addSeparator();
            popup.add(cb1);
            popup.add(cb2);
            popup.addSeparator();
            popup.add(displayMenu);
            displayMenu.add(errorItem);
            displayMenu.add(warningItem);
            displayMenu.add(infoItem);
            displayMenu.add(noneItem);
            popup.add(exitItem);
            return popup;
        }
    }
    

    As of Java 1.7, you can add the following line to remove the application bar from the taskbar:

    frame.setType(Type.UTILITY);
    
    0 讨论(0)
  • 2020-12-20 13:40

    you can add ActionListener to the TrayIcon, mouse double_click can showing JOptionPane

    trayIcon.addActionListener(new ActionListener() {
    
       @Override
       public void actionPerformed(ActionEvent e) {
           JOptionPane.showMessageDialog(null, "This dialog box is run from System Tray");
       }
    });
    
    0 讨论(0)
  • 2020-12-20 13:47

    I think you are looking for a MouseListener which you will add to your TrayIcon and will activate when a button on the mouse is clicked,moved etc. To get it to operate for left clicks only have a look at the ButtonMasks on MouseEvent (BUTTON1) being for left mouse clicks.

    0 讨论(0)
  • 2020-12-20 13:48

    You could read the official tutorial at http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html or check out http://weblogs.java.net/blog/ixmal/archive/2006/05/using_jpopupmen.html for a solution to use a jpopuomenu instead

    0 讨论(0)
  • 2020-12-20 13:50

    This should work:

    trayIcon.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            JOptionPane.showMessageDialog(null, "This shows after a left-click on tray icon");
        }
    });
    

    Override any other methods if you want a different kind of event (not just the click event from the example above).

    0 讨论(0)
提交回复
热议问题