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

avec Java Discussion :

Pb de variable de retour


Sujet :

avec Java

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Par défaut Pb de variable de retour
    Bonjour, je souhaiterai récupérer un numéro de code barre dans une variable NumCodeBarre mais je ne sais pas comment faire puisque la méthode serialEvent ne peut renvoyer qu'un void, pouvez-vous m'aider ?

    Merci d'avance.

    Voici mon code:
    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
    package Test;
     
    import javax.comm.*;
    import com.sun.comm.Win32Driver;
    import java.io.*;
    import java.util.*;
     
    public class GestionLecteur extends Thread implements SerialPortEventListener {
     
    	private CommPortIdentifier portId;
    	private SerialPort serialPort;
    	private BufferedReader fluxLecture;
    	private boolean running;
     
     
    	/**
             * Constructeur qui récupère l'identifiant du port et lance l'ouverture.
             */
    	public GestionLecteur(String port) {
    		//initialisation du driver
    		Win32Driver w32Driver = new Win32Driver();
    		w32Driver.initialize();
     
    		//récupération de l'identifiant du port
    		try {
    			portId = CommPortIdentifier.getPortIdentifier(port);
    		} catch (NoSuchPortException e) {
    		}
     
    		//ouverture du port
    		try {
    			serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
    		} catch (PortInUseException e) {
                        System.out.println(e.getMessage());
    		}
    		//récupération du flux
    		try {
    			fluxLecture =
    				new BufferedReader(
    					new InputStreamReader(serialPort.getInputStream()));
    		} catch (IOException e) {
    		}
    		//ajout du listener
    		try {
    			serialPort.addEventListener(this);
    		} catch (TooManyListenersException e) {
    		}
    		//paramétrage du port
    		serialPort.notifyOnDataAvailable(true);
    		try {
    			serialPort.setSerialPortParams(
    				9600,
    				SerialPort.DATABITS_8,
    				SerialPort.STOPBITS_1,
    				SerialPort.PARITY_NONE);
    		} catch (UnsupportedCommOperationException e) {
    		}
    		System.out.println("port ouvert, attente de lecture");
    	}
    	public void run() {
    		running = true;
    		while (running) {
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e) {
    			}
    		}
    		//fermeture du flux et port
    		try {
    			fluxLecture.close();
    		} catch (IOException e) {
    		}
    		serialPort.close();
    	}
    	/**
             * Méthode de gestion des événements.
             */
    	public void serialEvent(SerialPortEvent event) {
    		//gestion des événements sur le port :
    		//on ne fait rien sauf quand les données sont disponibles
    		switch (event.getEventType()) {
    			case SerialPortEvent.BI :
    			case SerialPortEvent.OE :
    			case SerialPortEvent.FE :
    			case SerialPortEvent.PE :
    			case SerialPortEvent.CD :
    			case SerialPortEvent.CTS :
    			case SerialPortEvent.DSR :
    			case SerialPortEvent.RI :
    			case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
    				break;
    			case SerialPortEvent.DATA_AVAILABLE :
    				String NumCodeBarre = new String(); 
    				try {
    					//lecture du buffer et affichage
    					NumCodeBarre = (String) fluxLecture.readLine();
     
     
    				} catch (IOException e) {
    				}
    				break;
    		}
    	}
     
     
    	/**
             * Permet l'arrêt du thread
             */
    	public void stopThread() {
    		running = false;
    	}
     
    }

    je fais appel dans mon IHM comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    new GestionLecteur("COM1");
    Merci encore.

  2. #2
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Bonjour, petite précision : void n'est pas un type, donc une méthode ne renvoi pas void

    Tu peux stoker la valeur de NumCodeBarre dans un attribut de la classe GestionLecteur, et tu fais une fonction (un getter) qui renverra cette valeur.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Par défaut
    mais comment faire un getter ? Je ne sais pas
    Merci encore

  4. #4
    Membre Expert Avatar de guigui5931
    Profil pro
    Chef de projet NTIC
    Inscrit en
    Avril 2006
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Âge : 38
    Localisation : France, Nord (Nord Pas de Calais)

    Informations professionnelles :
    Activité : Chef de projet NTIC
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Avril 2006
    Messages : 1 667
    Par défaut
    Tu peux déclarer un attribut private String numCodeBarre dans ta classe et le renseigner dans ta méthode serialEvent. Tu fais ensuite une méthode
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
    public String getNumCodeBarre()
    {
        return this.numCodeBarre;
    }

  5. #5
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Par défaut
    Voilà ce que j'ai fait:

    GestionLecteur:
    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
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
     
    /*
     * Interface_Gardien.java
     *
     * Created on 29 mai 2008, 10:34
     */
     
    package Test;
     
    import java.awt.Color;
    import java.lang.String;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.AbstractButton;
     
    /**
     *
     * @author  adminlocal
     */
    public class Interface_Gardien extends javax.swing.JFrame {
     
            GestionSystemeGardien ent = new GestionSystemeGardien();
        /** Creates new form Interface_Gardien */
        public Interface_Gardien() {
            initComponents();
     
            setTitle("Interface du Gardien");     
     
            //centrage fenêtre
            ent.centrageFenetre(this);
     
            //Affichage caméra
            ent.affichageCamera(jPanelCam);
     
             //Connexion au lecteur code barre
             //ent.connexionLecteur();
     
     
              GestionLecteur lec = new GestionLecteur("COM1");
              String Num = lec.getNumCodeBarre();
              jTextFieldNumCodeBarre.setText(Num);
        }
     
        /** 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.
         */
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jPanelCam = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jPanelPhoto = new javax.swing.JPanel();
            jPanelAlarme = new javax.swing.JPanel();
            jToggleButtonArretAlarme = new javax.swing.JButton();
            jLabel3 = new javax.swing.JLabel();
            jSeparator1 = new javax.swing.JSeparator();
            jLabelPorte = new javax.swing.JLabel();
            jLabel4 = new javax.swing.JLabel();
            jToggleButtonPorte = new javax.swing.JToggleButton();
            jButtonPassageBadge = new javax.swing.JButton();
            jTextFieldNumCodeBarre = new javax.swing.JTextField();
            jLabel5 = new javax.swing.JLabel();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem2 = new javax.swing.JMenuItem();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jPanelCam.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout jPanelCamLayout = new org.jdesktop.layout.GroupLayout(jPanelCam);
            jPanelCam.setLayout(jPanelCamLayout);
            jPanelCamLayout.setHorizontalGroup(
                jPanelCamLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 454, Short.MAX_VALUE)
            );
            jPanelCamLayout.setVerticalGroup(
                jPanelCamLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 383, Short.MAX_VALUE)
            );
     
            jLabel1.setText("Caméra extérieure:");
     
            jLabel2.setText("Photo de l'employé:");
     
            jPanelPhoto.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout jPanelPhotoLayout = new org.jdesktop.layout.GroupLayout(jPanelPhoto);
            jPanelPhoto.setLayout(jPanelPhotoLayout);
            jPanelPhotoLayout.setHorizontalGroup(
                jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 454, Short.MAX_VALUE)
            );
            jPanelPhotoLayout.setVerticalGroup(
                jPanelPhotoLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 390, Short.MAX_VALUE)
            );
     
            jPanelAlarme.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
     
            org.jdesktop.layout.GroupLayout jPanelAlarmeLayout = new org.jdesktop.layout.GroupLayout(jPanelAlarme);
            jPanelAlarme.setLayout(jPanelAlarmeLayout);
            jPanelAlarmeLayout.setHorizontalGroup(
                jPanelAlarmeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 100, Short.MAX_VALUE)
            );
            jPanelAlarmeLayout.setVerticalGroup(
                jPanelAlarmeLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(0, 100, Short.MAX_VALUE)
            );
     
            jToggleButtonArretAlarme.setText("Marche/Arrêt Alarme");
            jToggleButtonArretAlarme.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jToggleButtonArretAlarmeActionPerformed(evt);
                }
            });
     
            jLabel3.setText("Déclenchement alarme:");
     
            jLabelPorte.setText("La porte est fermée");
     
            jLabel4.setText("Simulation de la porte:");
     
            jToggleButtonPorte.setText("Ouverture Porte");
            jToggleButtonPorte.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jToggleButtonPorteActionPerformed(evt);
                }
            });
     
            jButtonPassageBadge.setText("Passage du badge");
            jButtonPassageBadge.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButtonPassageBadgeActionPerformed(evt);
                }
            });
     
            jTextFieldNumCodeBarre.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jTextFieldNumCodeBarreActionPerformed(evt);
                }
            });
     
            jLabel5.setText("Numéro du code barre:");
     
            jMenu1.setText("Fichier");
     
            jMenuItem1.setText("Quitter");
            jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem1ActionPerformed(evt);
                }
            });
            jMenu1.add(jMenuItem1);
     
            jMenuBar1.add(jMenu1);
     
            jMenu2.setText("Configuration");
            jMenu2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenu2ActionPerformed(evt);
                }
            });
     
            jMenuItem2.setText("Configuration matériel");
            jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem2ActionPerformed(evt);
                }
            });
            jMenu2.add(jMenuItem2);
     
            jMenuBar1.add(jMenu2);
     
            setJMenuBar(jMenuBar1);
     
            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(83, 83, 83)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanelPhoto, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                        .add(org.jdesktop.layout.GroupLayout.LEADING, jPanelCam, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                        .add(layout.createSequentialGroup()
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jToggleButtonArretAlarme)
                                        .add(layout.createSequentialGroup()
                                            .add(14, 14, 14)
                                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                                .add(jLabel3)
                                                .add(jPanelAlarme, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                                    .add(160, 160, 160))
                                .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 433, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(34, 34, 34))))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 191, Short.MAX_VALUE)
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jTextFieldNumCodeBarre)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel5)
                                    .add(jButtonPassageBadge))
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(jLabelPorte, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 103, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(jToggleButtonPorte)
                                        .add(jLabel4))))
                            .add(185, 185, 185)))
                    .add(38, 38, 38))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(45, 45, 45)
                            .add(jLabel1)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jPanelCam, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(layout.createSequentialGroup()
                            .add(164, 164, 164)
                            .add(jLabel3)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jPanelAlarme, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jToggleButtonArretAlarme)))
                    .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(jLabel2)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jPanelPhoto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(56, Short.MAX_VALUE))
                        .add(layout.createSequentialGroup()
                            .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(57, 57, 57)
                            .add(jLabel4)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jLabelPorte)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                            .add(jToggleButtonPorte)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 194, Short.MAX_VALUE)
                            .add(jButtonPassageBadge)
                            .add(18, 18, 18)
                            .add(jLabel5)
                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                            .add(jTextFieldNumCodeBarre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(61, 61, 61))))
            );
     
            pack();
        }// </editor-fold>
     
        private void jToggleButtonPorteActionPerformed(java.awt.event.ActionEvent evt) {                                                   
            // TODO add your handling code here:
     
            //Ouverture/Fermeture de la porte
            GestionGache porte = new GestionGache();
            AbstractButton bouton = (AbstractButton) evt.getSource();
            boolean selected = bouton.getModel().isSelected();
            if (selected) {
                porte.PorteOuverte(jLabelPorte);
            } else {
                porte.PorteFermee(jLabelPorte);
            }
        }                                                  
     
        private void jTextFieldNumCodeBarreActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
    }
     
        private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
            // TODO add your handling code here:
     
        }                                      
     
        private void jButtonPassageBadgeActionPerformed(java.awt.event.ActionEvent evt) {                                                    
            // TODO add your handling code here:
            try {
                // TODO add your handling code here:
                String NumCodeBarre = "9204521220089";
     
     
     
                //Téléchargement de la photo
                ent.downloadFichier(NumCodeBarre, "photo_employe.jpg", ".jpg");
     
                //Affichage photo
                ent.affichagePhoto(jPanelPhoto);
     
                //Téléchargement du son
                ent.downloadFichier(NumCodeBarre, "son.wav", ".wav");
     
                //Lecture du son
                ent.lectureSon();
     
                //Requete
                ent.requete(NumCodeBarre);
     
     
                ent.majHeureEntree("root", "root", "jdbc:mysql://10.104.100.57:3306/snecmajava", "org.gjt.mm.mysql.Driver", "9204521220089");
     
                //Connexion à la BdD
                /*GestionSystemGardien app = new GestionSystemGardien();
                app.ConnexionBdD("root", "root", "jdbc:mysql://10.104.100.57:3306/", "org.gjt.mm.mysql.Driver");*/
     
     
                /*GestionAutorisation a = new GestionAutorisation();
                a.HeureEntree();*/
     
            } catch (Exception ex) {
                Logger.getLogger(Interface_Gardien.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                                   
     
        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            System.exit(0);
        }                                          
     
        private void jToggleButtonArretAlarmeActionPerformed(java.awt.event.ActionEvent evt) {                                                         
            // TODO add your handling code here:
              //Marche/Arrêt de l'alarme
            GestionAlarme alarme = new GestionAlarme(Color.WHITE, Color.RED, 1000,
                    1000, jPanelAlarme);
            AbstractButton bouton = (AbstractButton) evt.getSource();
            boolean selected = bouton.getModel().isSelected();
            if (selected) {
                alarme.demarrer();
            } else {
                alarme.arreter(); 
            }
        }                                                        
     
        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            new Parametrage().setVisible(true);
            /*Parametrage param = new Parametrage();
            param.setVisible(true);*/
        }                                          
     
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Interface_Gardien().setVisible(true);
                }
            });
        }
     
        // Variables declaration - do not modify
        private javax.swing.JButton jButtonPassageBadge;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JLabel jLabel4;
        private javax.swing.JLabel jLabel5;
        private javax.swing.JLabel jLabelPorte;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JPanel jPanelAlarme;
        private javax.swing.JPanel jPanelCam;
        private javax.swing.JPanel jPanelPhoto;
        private javax.swing.JSeparator jSeparator1;
        private javax.swing.JTextField jTextFieldNumCodeBarre;
        private javax.swing.JButton jToggleButtonArretAlarme;
        private javax.swing.JToggleButton jToggleButtonPorte;
        // End of variables declaration
     
    }

    Je fais appel à ce code dans mon IHM comme ceci:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    GestionLecteur lec = new GestionLecteur("COM1");
              String Num = lec.getNumCodeBarre();
              jTextFieldNumCodeBarre.setText(Num);

    Mais apparemment j'ai un problème car je n'affiche aucun numéro de code barre dans mon champs text de l'IHM. Comment faire?

    Merci encore

  6. #6
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Peux-tu nous montrer le code de la classe GestionLecteur? as-tu affecté une valeur à numCodeBarre située dedans?

  7. #7
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2008
    Messages
    140
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 140
    Par défaut
    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
    package Test;
     
    import javax.comm.*;
    import com.sun.comm.Win32Driver;
    import java.io.*;
    import java.util.*;
     
    public class GestionLecteur extends Thread implements SerialPortEventListener {
     
    	private CommPortIdentifier portId;
    	private SerialPort serialPort;
    	private BufferedReader fluxLecture;
            private PrintStream serial_out = null;
    	private boolean running;
            private String NumCodeBarre;
     
    	/**
             * Constructeur qui récupère l'identifiant du port et lance l'ouverture.
             */
    	public GestionLecteur(String port) {
    		//initialisation du driver
    		Win32Driver w32Driver = new Win32Driver();
    		w32Driver.initialize();
     
    		//récupération de l'identifiant du port
    		try {
    			portId = CommPortIdentifier.getPortIdentifier(port);
    		} catch (NoSuchPortException e) {
    		}
     
    		//ouverture du port
    		try {
    			serialPort = (SerialPort) portId.open("ModeEvenement", 2000);
    		} catch (PortInUseException e) {
    		}
    		//récupération du flux
    		try {
    			fluxLecture =
    				new BufferedReader(
    					new InputStreamReader(serialPort.getInputStream()));
    		} catch (IOException e) {
    		}
    		//ajout du listener
    		try {
    			serialPort.addEventListener(this);
    		} catch (TooManyListenersException e) {
    		}
    		//paramétrage du port
    		serialPort.notifyOnDataAvailable(true);
    		try {
    			serialPort.setSerialPortParams(
    				9600,
    				SerialPort.DATABITS_8,
    				SerialPort.STOPBITS_1,
    				SerialPort.PARITY_NONE);
    		} catch (UnsupportedCommOperationException e) {
    		}
    		System.out.println("port ouvert, attente de lecture");
    	}
    	public void run() {
    		running = true;
    		while (running) {
    			try {
    				Thread.sleep(2000);
    			} catch (InterruptedException e) {
    			}
    		}
    		//fermeture du flux et port
    		try {
    			fluxLecture.close();
    		} catch (IOException e) {
    		}
    		serialPort.close();
    	}
    	/**
             * Méthode de gestion des événements.
             */
    	public void serialEvent(SerialPortEvent event) throws IOException {
    		//gestion des événements sur le port :
    		//on ne fait rien sauf quand les données sont disponibles
    		switch (event.getEventType()) {
    			case SerialPortEvent.DATA_AVAILABLE :
                                serial_out = new PrintStream(serialPort.getOutputStream(), true, "ISO-8859-1");
    				NumCodeBarre = new String(); 
    				try {
    					//lecture du buffer et affichage
    					NumCodeBarre = (String) fluxLecture.readLine();
     
    					//InterLC.Recup_Code.setText(codeBarre);
    				} catch (IOException e) {
    				}
    				break;
    		}
     
     
    	}
     
            public String getNumCodeBarre()
            {
                return this.NumCodeBarre;          
            }
     
    	/**
             * Permet l'arrêt du thread
             */
    	public void stopThread() {
    		running = false;
    	}
    	/**
             * Méthode principale de l'exemple.
             */
    	public static void main(String[] args) {
    		//Récuperation du port en argument
    		String port = "COM1";
    		//lancement de l'appli
    		GestionLecteur modeEve=new GestionLecteur(port);
    		modeEve.start();
    		//"interface utilisateur"
    		System.out.println("taper q pour quitter");
    		//construction flux lecture
    		BufferedReader clavier =
    			new BufferedReader(new InputStreamReader(System.in));
    		//lecture sur le flux entrée.
    		try {
    			String lu = clavier.readLine();
    			while (!lu.equals("esc")) {
    			}
    		} catch (IOException e) {
    		}
    		modeEve.stopThread();
    	}
    }

  8. #8
    Membre Expert
    Avatar de CheryBen
    Inscrit en
    Mai 2005
    Messages
    1 599
    Détails du profil
    Informations personnelles :
    Âge : 43

    Informations forums :
    Inscription : Mai 2005
    Messages : 1 599
    Par défaut
    Ca me parait normal, au moment où tu récupère numCodeBarre, l'événement appelant serialEvent n'a pas encore eu lieu. (enfin je pense)

    Il faut que ton JTextField se mette à jour seulement une fois que le lecteur a lu le code barre. Pour cela tu as plusieurs solutions :
    Tu créés un événement, et toute la gestion pour que GestionLecteur prévienne les classes qui sont à son écoute. (solution propre mais plutôt complexe si tu ne vois pas de quoi je parle)
    Tu passes en argument du constructeur de GestionLecteur le JTextfield, tu le mets en attribut de classe et tu appelleras la méthode setText directement dans GestionLecteur au moment voulu. (solution facile, rapide et plutôt à éviter en général)

Discussions similaires

  1. [Tableaux] Variable de retour en cas d'erreur
    Par laxe13 dans le forum Langage
    Réponses: 7
    Dernier message: 13/01/2009, 11h30
  2. Réponses: 3
    Dernier message: 10/06/2008, 15h59
  3. affichage variable avec retour chariot
    Par swissmade dans le forum Langage
    Réponses: 1
    Dernier message: 19/03/2007, 19h02
  4. Récupération d'une variable de retour
    Par BONNEFOI Patrick dans le forum Bases de données
    Réponses: 3
    Dernier message: 26/02/2007, 08h58
  5. variable de retour d'une procedure stocke
    Par yahia dans le forum MS SQL Server
    Réponses: 2
    Dernier message: 14/09/2004, 14h12

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