Visualizzazione dei risultati da 1 a 7 su 7

Discussione: JPopupMenu e trayicon

Visualizzazione discussione

  1. #3
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    windows 7
    codice:
      package jcallremember.server;
    
    //~--- JDK imports ------------------------------------------------------------
    
    
    import java.awt.AWTException;
    import java.awt.Dimension;
    import java.awt.Image;
    import java.awt.SystemTray;
    import java.awt.Toolkit;
    import java.awt.TrayIcon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    
    
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    
    import javax.swing.JFrame;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.SwingUtilities;
    
    
    /**
     *
     * @author LINUX
     * Questa classe implementa graficamente la trayicon nella barra di sistema
     * che permette di accedere ad alcune funzioni del software
     */
    public class ServerSystemTray extends JFrame implements Runnable {
        private final Thread   T;
        private ServerListener serverListener;
        private JPopupMenu     popup;
        private SystemTray     systray;
        private JMenuItem      listaClientConnessi;
        private JMenuItem      menuPrincipale;
        private JMenuItem      esci;
        private JMenuItem      start_and_stop_server;
        private TrayIcon       trayicon;
        private ReentrantLock  lock;
        private Condition      c;
    
    
        public ServerSystemTray() throws IOException {
            this.setPreferredSize(new Dimension(100, 100));
            T = new Thread(this);
            this.lock = new ReentrantLock();
            this.c = lock.newCondition();
            this.serverListener=new ServerListener(this.lock,this.c);
            this.serverListener.start();
        }
    
    
        public ServerSystemTray(ReentrantLock lock, Condition c, ServerListener sl) 
        {
            this.setPreferredSize(new Dimension(100, 100));
            T                   = new Thread(this);
            this.lock           = lock;
            this.c              = c;
            this.serverListener = sl;
            sl.start();
        }
    
    
        public ServerListener getServerListener() {
            return serverListener;
        }
    
    
        public void setServerListener(ServerListener serverListener) {
            this.serverListener = serverListener;
        }
    
    
        public JPopupMenu getPopup() {
            return popup;
        }
    
    
        public void setPopup(JPopupMenu popup) {
            this.popup = popup;
        }
    
    
        public JMenuItem getStartAndStopServerButton() {
            return start_and_stop_server;
        }
    
    
        public void setStartAndStopServerButton(JMenuItem startandstopserver) {
            this.start_and_stop_server = startandstopserver;
        }
    
    
        public ReentrantLock getLock() {
            return lock;
        }
    
    
        public void setLock(ReentrantLock lock) {
            this.lock = lock;
        }
    
    
        public Condition getCondition() {
            return c;
        }
    
    
        public void setCondition(Condition c) {
            this.c = c;
        }
    
    
        public void start() {
            T.start();
        }
    
    
        /**
         * Inizializza i Listener degli oggetti JMenuItem
         */
        private void inizializzaListener() {
            // Listener item esci
            this.esci.addActionListener(new ActionListener(){
                @Override
                public void  actionPerformed(ActionEvent evt){
                    ServerSystemTray.this.getPopup().setVisible(false);
                    ServerSystemTray.this.getServerListener().requestClose();
                    ServerSystemTray.this.getLock().lock();
                    try {
                        ServerSystemTray.this.getCondition().signalAll();
                    }finally {
                        ServerSystemTray.this.getLock().unlock();
                        System.exit(0);
                    }
                }
            });
            this.start_and_stop_server.addActionListener(new ServerStartStopListener(this));
            this.listaClientConnessi.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
    
    
    
    
                }
            });
            this.menuPrincipale.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!MainMenu.isOpen()) {
                        SwingUtilities.invokeLater(new MainMenu());
                    }
    
    
                    ServerSystemTray.this.popup.setVisible(false);
                }
            });
            trayicon.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        if (popup.isVisible()) {
                            popup.setVisible(false);
                        } else {
                            popup.show(e.getComponent(), e.getX(), e.getY());
                            
                        }
                    }
    
    
                    if (e.getButton() == 3) {
                        popup.show(e.getComponent(), e.getX(), e.getY());
                        
                    }
                }
            });
        }
    
    
        /**
         * Inizializza La SystemTray e il relativo menu popup
         * @throws AWTException
         */
        public void setup() throws AWTException {
            if (SystemTray.isSupported()) {
                systray = SystemTray.getSystemTray();
                Image img = Toolkit.getDefaultToolkit().getImage("call.png");
                trayicon = new TrayIcon(img);
                systray.add(trayicon);
            }
    
    
            popup                    = new JPopupMenu();
            this.listaClientConnessi = new JMenuItem("Client Collegati");
            this.menuPrincipale      = new JMenuItem("Main Menu");
            this.start_and_stop_server = new JMenuItem("Interrompi Server");
            this.esci=new JMenuItem("Esci");
            this.popup.add(this.listaClientConnessi);
            this.popup.add(this.menuPrincipale);
            this.popup.add(start_and_stop_server);
            this.popup.add(this.esci);
            this.inizializzaListener();
        }
    
    
        @Override
        public void run() {
            try {
                this.setup();
            } catch (AWTException ex) {
                Logger.getLogger(ServerSystemTray.class.getName()).
                        log(Level.SEVERE, null, ex);
            }
        }
    }
    Considerare comunque che ci vorrebbe un PopupMenu e non un JPopupMenu
    Ultima modifica di linux_r; 05-06-2014 a 18:48

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.