IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Systray : mettre en icone caché


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut Systray : mettre en icone caché
    Bonjour,

    Actuellement, j'arrive a mettre mon application dans la systray en utilisant la classe SystemTray.
    Est-il possible de mettre l'icone dans la partie "icones cachés" de la systray de windows ?

    Aussi, est-il possible dans mon Popup d'ajouter un element cochable ? a priori, il semblerait qu'il faudrait utiliser un JPopupMenu mais ce n'est pas compatible avec la classe SystemTray, non ?


    Aussi, a l'ouverture de mon application, est-il possible de savoir si elle a déja été ouverte ?

  2. #2
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Citation Envoyé par boboss123 Voir le message
    1. Est-il possible de mettre l'icone dans la partie "icones cachés" de la systray de windows ?
    2. Est-il possible dans mon Popup d'ajouter un element cochable ?
    3. A l'ouverture de mon application, est-il possible de savoir si elle a déja été ouverte ?
    1. Je ne connais pas de solution Java, mais la page suivante pourrait t'aider:
    http://ask-leo.com/how_do_i_make_sys...rmanently.html

    2. Tu peux utiliser un "PopupMenu" avec un "CheckboxMenuItem".

    3. Voici un exemple (avec possibilité de communications entre les applications):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    import java.io.*;
     
    public class Example {
     
        private SingleInstanceController sic = null;
     
        public Example() {
     
            sic = new SingleInstanceController(new File(System.getProperty(
                    "java.io.tmpdir") + "Example.file"), "sic_example_application");
            if (sic.isOtherInstanceRunning()) {
    //            sic.sendMessageToRunningApplication("You're still running!?");
    //            sic.sendMessageToRunningApplication("ByeBye");
                System.exit(0);
            } else {
                sic.registerApplication();
    //            sic.addApplicationStartedListener(new ApplicationStartedListener() {
    //
    //                public void applicationStarted() {
    //                    System.out.println("First Instance: An other instance started");
    //                }
    //
    //                public void foreignApplicationStarted(final String name) {
    //                    System.out.println("First Instance: A foreign application "
    //                            + "started on this port");
    //                }
    //
    //                public void messageArrived(final Object obj) {
    //                    System.out.println("Other Instance: " + obj.toString());
    //                    if (obj.equals("You're still running!?")) {
    //                        System.out.println("First Instance: Yes, of course!");
    //                    } else if (obj.equals("ByeBye")) {
    //                        System.out.println("First Instance: bye ...");
    //                    }
    //                }
    //            });
            }
        }
     
        public static void main(String[] args) {
            new Example();
        }
    }
    Tu doit installer les classes suivantes pour pouvoir utiliser l'exemple:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public interface ApplicationStartedListener {
     
        void applicationStarted();
     
        void foreignApplicationStarted(String name);
     
        void messageArrived(Object obj);
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public class ClassCheck implements Serializable {
     
        private static final long serialVersionUID = 1L;
        private String className = null;
     
        public ClassCheck(String className) {
            setClassName(className);
        }
     
        public ClassCheck() {
        }
     
        @Override
        public String toString() {
            return this.className;
        }
     
        public String getClassName() {
            return this.className;
        }
     
        public void setClassName(String className) {
            this.className = className;
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    import java.io.*;
    import java.net.*;
    import java.util.*;
     
    public class SingleInstanceController {
     
        private boolean result = false;
        private File file = null;
        private ObjectOutputStream oos = null;
        private ObjectInputStream ois = null;
        private ServerSocket server = null;
        private Socket client = null;
        private ArrayList<ApplicationStartedListener> listener = null;
        private String appname = null;
     
        public SingleInstanceController(File file, String appname) {
     
            this.file = file;
            this.appname = appname;
            this.listener = new ArrayList<ApplicationStartedListener>();
        }
     
        public SingleInstanceController(String appname) {
            this(new File(System.getProperty("java.io.tmpdir") + "/923jhakE53Kk9235b43.6m7"), appname);
        }
     
        public void addApplicationStartedListener(ApplicationStartedListener asl) {
            this.listener.add(asl);
        }
     
        public void removeApplicationStartedListener(ApplicationStartedListener asl) {
            this.listener.remove(asl);
        }
     
        public boolean isOtherInstanceRunning() {
     
            if (!this.file.exists()) {
                return false;
            }
            return sendMessageToRunningApplication(new ClassCheck(this.appname));
        }
     
        public boolean sendMessageToRunningApplication(final Object obj) {
     
            this.result = false;
            try {
                this.client = new Socket("localhost", getPortNumber());
                // In einem neuen Thread kommunizieren, um einen Deadlock zu verhindern
                new Thread(new Runnable() {
     
                    public void run() {
                        try {
                            SingleInstanceController.this.oos = new ObjectOutputStream(SingleInstanceController.this.client.getOutputStream());
                            SingleInstanceController.this.ois = new ObjectInputStream(SingleInstanceController.this.client.getInputStream());
                            SingleInstanceController.this.oos.writeObject(obj);
                            SingleInstanceController.this.oos.flush();
                            SingleInstanceController.this.result = SingleInstanceController.this.ois.readBoolean();
                        } catch (IOException e) {
                            SingleInstanceController.this.result = false;
                        }
                    }
                }).start();
                for (int i = 0; i < 10; i++) {
                    if (this.result == true) {
                        break;
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                this.client.close();
                return this.result;
            } catch (IOException e) {
                return false;
            }
        }
     
        public boolean registerApplication() {
     
            try {
                if (!this.file.exists()) {
                    if (!this.file.getParentFile().mkdirs() && !this.file.getParentFile().exists()) {
                        return false;
                    }
                    if (!this.file.createNewFile()) {
                        return false;
                    }
                }
                BufferedWriter wuffy = new BufferedWriter(new FileWriter(this.file));
                int port = getFreeServerSocket();
                if (port != -1) {
                    startServer();
                }
                wuffy.write(String.valueOf(port));
                wuffy.close();
                return true;
            } catch (IOException e) {
                return false;
            }
        }
     
        protected void messageArrived(Object obj) {
     
            for (ApplicationStartedListener asl : this.listener) {
                asl.messageArrived(obj);
            }
        }
     
        protected void applicationStartet() {
     
            for (ApplicationStartedListener asl : this.listener) {
                asl.applicationStarted();
            }
        }
     
        protected void foreignApplicationStarted(String name) {
     
            for (ApplicationStartedListener asl : this.listener) {
                asl.foreignApplicationStarted(name);
            }
        }
     
        private int getPortNumber() {
     
            try {
                BufferedReader buffy = new BufferedReader(new FileReader(this.file));
                int port = Integer.parseInt(buffy.readLine().trim());
                buffy.close();
                return port;
            } catch (Exception e) {
                return -1;
            }
        }
     
        private void startServer() {
     
            new Thread(new Runnable() {
     
                public void run() {
                    while (true) {
                        try {
                            SingleInstanceController.this.client = SingleInstanceController.this.server.accept();
                            if (SingleInstanceController.this.client.getInetAddress().isLoopbackAddress()) {
                                new Thread(new Runnable() {
     
                                    public void run() {
                                        try {
                                            SingleInstanceController.this.oos = new ObjectOutputStream(SingleInstanceController.this.client.getOutputStream());
                                            SingleInstanceController.this.ois = new ObjectInputStream(SingleInstanceController.this.client.getInputStream());
                                            Object obj = SingleInstanceController.this.ois.readObject();
                                            if (obj instanceof ClassCheck) {
                                                if (obj.toString().equals(SingleInstanceController.this.appname)) {
                                                    SingleInstanceController.this.oos.writeBoolean(true);
                                                    applicationStartet();
                                                } else {
                                                    SingleInstanceController.this.oos.writeBoolean(false);
                                                    foreignApplicationStarted(obj.toString());
                                                }
                                            } else {
                                                messageArrived(obj);
                                                SingleInstanceController.this.oos.writeBoolean(true);
                                            }
                                            SingleInstanceController.this.oos.flush();
                                            SingleInstanceController.this.client.close();
                                        } catch (IOException e) {
                                            e.printStackTrace();
                                        } catch (ClassNotFoundException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }).start();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
     
        private int getFreeServerSocket() {
     
            for (int i = 2000; i < 10000; i++) {
                try {
                    this.server = new ServerSocket(i);
                    return i;
                } catch (IOException ignore) {
                }
            }
            return -1;
        }
    }

    SOURCE: http://www.java-blog-buch.de/d-java-...l-ausfuhren/2/

  3. #3
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    ok merci beaucoup

    pour la detection d'application ouverte, je voudrais que si l'application est déja ouverte que :
    1- ça ferme l'application que j'ai essayer d'ouvrir (c'est ce que fait ton code actuel)
    2- que ça active la fenetre de l'application qui est déja ouverte

    => c'est possible ?

    Ce code est bien compatible multi-plateformes ?

  4. #4
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Citation Envoyé par boboss123 Voir le message
    pour la détection d'application ouverte, je voudrais que si l'application est déjà ouverte que :
    1- ça ferme l'application que j'ai essayer d'ouvrir (c'est ce que fait ton code actuel)
    2- que ça active la fenêtre de l'application qui est déjà ouverte
    => c'est possible ?
    Ce code est bien compatible multi-plateformes ?
    Le code est bien compatible multi-plateformes. Comme le montre la partie mise en commentaire, tu peux envoyer un message par "sendMessageToRunningApplication". La première application réagit dans la méthode "messageArrived" de son "ApplicationStartedListener" et peut essayer d'activer sa fenêtre. Par contre je ne suis pas sûr s'il existe également une solution multi-plateformes pour justement activer la fenêtre.

  5. #5
    Membre chevronné
    Profil pro
    Inscrit en
    Septembre 2009
    Messages
    1 855
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2009
    Messages : 1 855
    Par défaut
    ok merci beaucoup l'envoie de message fonctionne.

    Par contre je ne vois pas comment faire pour activer l'application : personne n'a une idée ?

  6. #6
    Membre expérimenté Avatar de uhrand
    Profil pro
    Développeur informatique
    Inscrit en
    Octobre 2009
    Messages
    203
    Détails du profil
    Informations personnelles :
    Localisation : Luxembourg

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Octobre 2009
    Messages : 203
    Par défaut
    Citation Envoyé par boboss123 Voir le message
    je ne vois pas comment faire pour activer l'application : personne n'a une idée ?
    As-tu essayé Window#toFront() ?

Discussions similaires

  1. Comment mettre mon programme dans les 'icones cachées'
    Par Simonake dans le forum Agents de placement/Fenêtres
    Réponses: 2
    Dernier message: 31/01/2010, 02h19
  2. [Win32] Mettre une icone
    Par Marineee dans le forum MFC
    Réponses: 14
    Dernier message: 02/01/2008, 14h52
  3. mettre une icone dans le systray
    Par Jérémy Lefevre dans le forum Windows
    Réponses: 2
    Dernier message: 09/07/2007, 16h21
  4. Comment mettre une icone dans un bouton ?
    Par Hokagge dans le forum MFC
    Réponses: 7
    Dernier message: 29/06/2005, 20h28
  5. [MFC] mettre des icones sur un CTreeCtrl
    Par Toutouffe dans le forum MFC
    Réponses: 3
    Dernier message: 30/11/2004, 17h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo