Visualizzazione dei risultati da 1 a 7 su 7

Discussione: JPopupMenu e trayicon

  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305

    JPopupMenu e trayicon

    Salve sto provando ad utilizzare un JPopupMenu con una trayicon, il problema nasce nel fatto che il popup menu (che funziona perfettamente) non mostra la selezione quando mi muovo sulle varie scelte. Per selezione intendo la "luce di selezione" che si muove sui vari item del popup. grazie

  2. #2
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    Salve sto provando ad utilizzare un JPopupMenu con una trayicon, il problema nasce nel fatto che il popup menu (che funziona perfettamente) non mostra la selezione quando mi muovo sulle varie scelte. Per selezione intendo la "luce di selezione" che si muove sui vari item del popup. grazie
    Senza vedere il codice che hai usato comunque è un po' difficile capire cosa potrebbe non andare o non essere appropriato.
    Precisa anche piattaforma/sistema operativo, dato che la traybar è molto dipendente dal S.O. (come si comporta, presenta, ecc...).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  3. #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

  4. #4
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    Ho risolto cosi ma non capisco il perchè
    codice:
     trayicon.addMouseListener(new MouseAdapter() {
    
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    if (e.isPopupTrigger()) {
                        popup.setInvoker(popup); //QUESTO METODO PERMETTE DI RISOLVERE IL PROBLEMA
                        popup.setLocation(e.getX(), e.getY());
                        popup.setVisible(true);
                    }
                }
    
    
            });

  5. #5
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    L'impostazione basilare/minima di un menù di popup ad un TrayIcon è quella descritta nel tutorial How to Use the System Tray

    Ovvero:

    trayIcon.setPopupMenu(popup);

    Una riga. Stop. Tutto qui (niente listener, test, eventi ...).
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

  6. #6
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    Quote Originariamente inviata da andbin Visualizza il messaggio
    L'impostazione basilare/minima di un menù di popup ad un TrayIcon è quella descritta nel tutorial How to Use the System Tray

    Ovvero:

    trayIcon.setPopupMenu(popup);

    Una riga. Stop. Tutto qui (niente listener, test, eventi ...).
    si ma io ho usato un jpopupmenu !!

  7. #7
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,284
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    si ma io ho usato un jpopupmenu !!
    Eh beh ... hai ragione pure tu!

    Comunque qui Using JPopupMenu in TrayIcon fa vedere appunto l'uso di setInvoker e ne spiega anche il motivo. Sebbene mi pare di capire che comunque non è tutto "rose e fiori" con JPopupMenu.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    java.util.function Interfaces Cheat SheetJava Versions Cheat Sheet

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.