Example usage for java.awt PopupMenu add

List of usage examples for java.awt PopupMenu add

Introduction

In this page you can find the example usage for java.awt PopupMenu add.

Prototype

public MenuItem add(MenuItem mi) 

Source Link

Document

Adds the specified menu item to this menu.

Usage

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*w  ww . ja v a  2  s.  c  o  m*/
    }

    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");

    PopupMenu menu = new PopupMenu();

    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("asdf");
        }
    });
    menu.add(messageItem);

    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);

    tray.add(icon);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;// www  .  ja va2 s  .c o  m
    }

    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");

    PopupMenu menu = new PopupMenu();

    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("asdf");
        }
    });
    menu.add(messageItem);

    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);

    icon.setToolTip("tooltip");
    tray.add(icon);
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;/*  w  ww .ja  v a 2s .co m*/
    }

    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");

    PopupMenu menu = new PopupMenu();

    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println("asdf");
        }
    });
    menu.add(messageItem);

    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);

    image = toolkit.getImage("anotherImage.jpg");
    icon.setImage(image);
    tray.add(icon);
}

From source file:SystemTrayDemo.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        System.out.println("SystemTray is not supported");
        return;//w  w  w . j  a v  a  2  s .c om
    }

    SystemTray tray = SystemTray.getSystemTray();
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Image image = toolkit.getImage("trayIcon.jpg");

    PopupMenu menu = new PopupMenu();

    MenuItem messageItem = new MenuItem("Show Message");
    messageItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "www.java2s.com");
        }
    });
    menu.add(messageItem);

    MenuItem closeItem = new MenuItem("Close");
    closeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    menu.add(closeItem);
    TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu);
    icon.setImageAutoSize(true);

    tray.add(icon);
}

From source file:SystemTrayTest.java

public static void main(String[] args) {
    final TrayIcon trayIcon;

    if (!SystemTray.isSupported()) {
        System.err.println("System tray is not supported.");
        return;//  ww w  .  j av a 2s  .  c  o m
    }

    SystemTray tray = SystemTray.getSystemTray();
    Image image = Toolkit.getDefaultToolkit().getImage("cookie.png");

    PopupMenu popup = new PopupMenu();
    MenuItem exitItem = new MenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });
    popup.add(exitItem);

    trayIcon = new TrayIcon(image, "Your Fortune", popup);

    trayIcon.setImageAutoSize(true);
    trayIcon.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            trayIcon.displayMessage("How do I turn this off?",
                    "Right-click on the fortune cookie and select Exit.", TrayIcon.MessageType.INFO);
        }
    });

    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.err.println("TrayIcon could not be added.");
        return;
    }

    final List<String> fortunes = readFortunes();
    Timer timer = new Timer(10000, new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int index = (int) (fortunes.size() * Math.random());
            trayIcon.displayMessage("Your Fortune", fortunes.get(index), TrayIcon.MessageType.INFO);
        }
    });
    timer.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;/*ww w .  j  av a2 s  .co m*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2", popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;//from  w  w  w .j  a v a 2  s  .  c  om
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2");
    ti.setPopupMenu(popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);
    tray.add(ti);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;//from   w  ww.j a v  a2  s .c  o m
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2");
    ti.setPopupMenu(popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);

    String tooltip = ti.getToolTip();

    tray.add(ti);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;//from   ww  w.j a va2s  .  c o  m
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2");
    ti.setPopupMenu(popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);

    PopupMenu popupMenu = ti.getPopupMenu();

    tray.add(ti);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    if (!SystemTray.isSupported()) {
        return;/*from   ww w. j a  v a  2 s . c o m*/
    }
    SystemTray tray = SystemTray.getSystemTray();

    Dimension size = tray.getTrayIconSize();
    BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();

    g.setColor(Color.blue);
    g.fillRect(0, 0, size.width, size.height);

    PopupMenu popup = new PopupMenu();
    MenuItem miExit = new MenuItem("Exit");
    ActionListener al;
    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    };
    miExit.addActionListener(al);
    popup.add(miExit);

    TrayIcon ti = new TrayIcon(bi, "System Tray Demo #2");
    ti.setPopupMenu(popup);

    al = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
        }
    };
    ti.setActionCommand("My Icon");
    ti.addActionListener(al);

    MouseListener ml = new MouseListener() {
        public void mouseClicked(MouseEvent e) {
            System.out.println("Tray icon: Mouse clicked");
        }

        public void mouseEntered(MouseEvent e) {
            System.out.println("Tray icon: Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Tray icon: Mouse exited");
        }

        public void mousePressed(MouseEvent e) {
            System.out.println("Tray icon: Mouse pressed");
        }

        public void mouseReleased(MouseEvent e) {
            System.out.println("Tray icon: Mouse released");
        }
    };
    ti.addMouseListener(ml);
    MouseMotionListener mml = new MouseMotionListener() {
        public void mouseDragged(MouseEvent e) {
            System.out.println("Tray icon: Mouse dragged");
        }

        public void mouseMoved(MouseEvent e) {
            System.out.println("Tray icon: Mouse moved");
        }
    };
    ti.addMouseMotionListener(mml);

    boolean bool = ti.isImageAutoSize();

    tray.add(ti);
}