0

I created the following code using JAVA to run on the Notification area. When I run the program, it runs perfectly. But I want a context menu on its icon in the notification area when I clicked or right clicked on it. For example, if I clicked on the icon I want a context menu with "exit", "settings" and "about".

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*Developed by Mr.S.SAKTHY*/

package hidden_eye;
import com.sun.speech.freetts.*;
import java.awt.AWTException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import java.awt.Robot;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
//import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
//import javax.swing.ImageIcon;

/*

*/


/**
 *
 * @author Jaso
 */
public class eye_capture extends javax.swing.JFrame {

    /**
     * Creates new form eye_capture
     */
    int c= 0;
    Timer b= new Timer(50,new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent ae) {
            if (c > 100){
             // b.stop();
              setVisible(false);

                //Folder Creation......//
                Date now = new Date();
                 File main_Folder = new File("D:\\"+"HE_Capture");
                 main_Folder.mkdir();
                 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd E hh.a");
                 String time = dateFormat.format(now);
                 File dir = new File("D:\\HE_Capture\\"+time);
                 dir.mkdir();
                //Screen Capture.........//
                   SimpleDateFormat dateFormat1 = new SimpleDateFormat("hh.mm a");
                   String time1 = dateFormat1.format(now);
                  try{
                     BufferedImage image = new Robot().createScreenCapture(new Rectangle

(Toolkit.getDefaultToolkit().getScreenSize()));
                     ImageIO.write(image, "PNG", new File(dir+"\\"+time1+"_Eye.png"));
                     //""+dir+"\\"+time1+"_Eye.png"
                  }
                  catch(Exception e){
                      e.printStackTrace();
                  }
            }
            //incress the timer time....//
            jpbEye.setValue(c++) ;
        }
    });

    public eye_capture() {
        setUndecorated(true);
        b.start();
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        btnExit = new javax.swing.JButton();
        jpbEye = new javax.swing.JProgressBar();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowDeactivated(java.awt.event.WindowEvent evt) {
                formWindowDeactivated(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                formComponentHidden(evt);
            }
        });
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                formWindowStateChanged(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(102, 102, 102));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jPanel1MouseMoved(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setBackground(new java.awt.Color(51, 51, 51));
        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(51, 51, 51));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Screen Capture");
        jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
        );

        btnExit.setBackground(new java.awt.Color(0, 153, 153));
        btnExit.setText("Exit");
        btnExit.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnExit.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                btnExitMouseMoved(evt);
            }
        });
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jpbEye.setBackground(new java.awt.Color(255, 255, 255));
        jpbEye.setForeground(new java.awt.Color(0, 204, 204));
        jpbEye.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 

102)));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 81, 

javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpbEye, javax.swing.GroupLayout.DEFAULT_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup

(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 31, 

javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpbEye, javax.swing.GroupLayout.PREFERRED_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 9, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 

javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>                        
private static final String VOICENAME="kevin16";
    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {                              


        // TODO add your handling code here:
        System.exit(1);
    }                                       

    private void formWindowActivated(java.awt.event.WindowEvent evt) {                                 



/*----------------------1st---------------------------*/

//    Toolkit mainToolkit = Toolkit.getDefaultToolkit();
//    SystemTray mainTray = SystemTray.getSystemTray();
//    Image trayIconImage = mainToolkit.createImage(getClass().getResource("fu_i3.png"));
//    TrayIcon mainTrayIcon = new TrayIcon(trayIconImage);
//        try {    
//            mainTray.add(mainTrayIcon);
//        } catch (AWTException ex) {
//            Logger.getLogger(eye_capture.class.getName()).log(Level.SEVERE, null, ex);
//        }

/*---------------------2nd----------------------------*/

        SystemTray tray = SystemTray.getSystemTray();
        ImageIcon icon = new ImageIcon(getClass().getResource("cam2.png"));
        Image image = icon.getImage();
        TrayIcon trayIcon = new TrayIcon(image, "Tray Demo");
        trayIcon.setImageAutoSize(true);
        trayIcon.setToolTip("Hidden_EYE");
        try {
            tray.add(trayIcon);
        } catch (AWTException ex) {
            Logger.getLogger(eye_capture.class.getName()).log(Level.SEVERE, null, ex);
        }
        trayIcon.displayMessage("HIDDEN_EyE", "Start Monitoring", MessageType.INFO);
        close();



    //Infomar.............//
         Voice voice;
            VoiceManager vm=VoiceManager.getInstance();
            voice=vm.getVoice(VOICENAME);
            voice.allocate();
            try{
            voice.speak("Now you are monitoring on your system in hidden eye pro");
            }
            catch(Exception e){
            } 

    }                                    

    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        // TODO add your handling code here:
    }                                 

    private void formComponentHidden(java.awt.event.ComponentEvent evt) {                              


        // TODO add your handling code here:
    }                                    

    private void btnExitMouseMoved(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void jPanel1MouseMoved(java.awt.event.MouseEvent evt) {                                   
        // TODO add your handling code here:
    }                                  

    private void formWindowStateChanged(java.awt.event.WindowEvent evt) {                              


        // TODO add your handling code here:
    }                                       

    private void formWindowDeactivated(java.awt.event.WindowEvent evt) {                               


        // TODO add your handling code here:
    }                                      

    public void close()
  {
    JOptionPane.showMessageDialog(null,"Exiting code...");
    System.exit(0);
  }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : 

javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(eye_capture.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(eye_capture.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(eye_capture.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(eye_capture.class.getName()).log

(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new eye_capture().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnExit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JProgressBar jpbEye;
    // End of variables declaration                   
}
S.Sakthybaalan
  • 499
  • 6
  • 20
  • Have you looked at https://stackoverflow.com/questions/766956/how-do-i-create-a-right-click-context-menu-in-java-swing ...and... http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup ? What problem are you having? – markspace Aug 12 '17 at 16:26
  • `How to create a context menu at notification area` - see [How to Use the System Tray](http://docs.oracle.com/javase/tutorial/uiswing/misc/systemtray.html) – camickr Aug 12 '17 at 16:34

0 Answers0