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

Java Discussion :

Lien entre deux classe


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut Lien entre deux classe
    Bonjour, je suis occupé de faire un programme sous NetBeans et j'ai 2 fichier l'un pour mon interface graphique l'autre pour me connecter à mon port com et attendre que des données arrivent. Le problème est que lorsque je lance mon projet (les 2 fichiers sont dans le meme block) seul mon interface graphique se lance. Il doit exister un moment pour exécuter les 2 en meme temps en mettant une ligne dans mon programme main(interface) non?

    Voici mes 2 fichier :

    Main : (interface graphique)
    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
     
    package blackbox;
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
     
    public class Main extends javax.swing.JFrame
    {
        static CommPortIdentifier portId;
        static Enumeration          portList;
     
        public Main()
        {
            initComponents();
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
        private void initComponents() {
     
            jComboBox1 = new javax.swing.JComboBox();
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            jButton1.setText("Test Connection");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
     
            jButton2.setText("Lister les Ports");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
     
            jTextArea1.setColumns(20);
            jTextArea1.setRows(5);
            jScrollPane1.setViewportView(jTextArea1);
     
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 276, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jButton2)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jButton1)))
                    .addContainerGap())
            );
     
            pack();
        }// </editor-fold>                       
     
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
     
    }                                       
     
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements())
        {
            portId = (CommPortIdentifier) portList.nextElement();
            jComboBox1.addItem(portId.getName());
        }
    }                                       
        public static void main(String args[])
        {
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Main().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                    
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        // End of variables declaration
    }
    Read : (connection port com et attente données)
    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
     
    package blackbox;
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
     
    public class Read implements Runnable, SerialPortEventListener {
        static CommPortIdentifier portId;
        static Enumeration          portList;
        InputStream              inputStream;
        SerialPort              serialPort;
        Thread              readThread;
     
        public static void main(String[] args) {
        boolean              portFound = false;
        String              defaultPort = "/dev/ttyS0";
     
         if (args.length > 0) {
            defaultPort = args[0];
        }
     
        portList = CommPortIdentifier.getPortIdentifiers();
     
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (portId.getName().equals(defaultPort)) {
                System.out.println("Found port: "+defaultPort);
                portFound = true;
                Read reader = new Read();
            }
            }
        }
        if (!portFound) {
            System.out.println("port " + defaultPort + " not found.");
        }
     
        }
     
        public Read() {
        try {
            serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
        } catch (PortInUseException e) {}
     
        try {
            inputStream = serialPort.getInputStream();
        } catch (IOException e) {}
     
        try {
            serialPort.addEventListener(this);
        } catch (TooManyListenersException e) {}
     
        serialPort.notifyOnDataAvailable(true);
     
        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                           SerialPort.STOPBITS_1,
                           SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {}
     
        readThread = new Thread(this);
     
        readThread.start();
        }
     
        public void run() {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {}
        }
     
        public void serialEvent(SerialPortEvent event) {
        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:
     
            byte[] readBuffer = new byte[20];
            try {
            while (inputStream.available() > 0)
                    {
                        int numBytes = inputStream.read(readBuffer);
            }
     
            System.out.print(new String(readBuffer));
            } catch (IOException e) {}
     
            break;
        }
        }
     
    }
    merci d'avance

  2. #2
    Membre Expert
    Avatar de gifffftane
    Profil pro
    Inscrit en
    Février 2007
    Messages
    2 354
    Détails du profil
    Informations personnelles :
    Localisation : France, Loire (Rhône Alpes)

    Informations forums :
    Inscription : Février 2007
    Messages : 2 354
    Par défaut
    Oui, il faut que tu décides laquelle des deux classes sera la principale.

    Dans cette classe, tu devras écrire le code qui lancera l'autre et procéder au contrôle de cette autre.

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

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut
    Ok daccord, c'est bien ce qu'il me semblait.

    Alors ma classe principale sera Main et Read la suivante comment je fait pour que Read s'exécute lorsque je clique sur un boutton?


    Merci de votre aide

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut
    je fait comment pour lancer la 2eme quand je clique sur un boutton de la première svp
    merci

  5. #5
    Membre émérite

    Étudiant
    Inscrit en
    Octobre 2007
    Messages
    510
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2007
    Messages : 510
    Par défaut
    bonjour

    parfait alors deux choses :

    si ta classe main est la pricipale alors ta méthode main doit s'y trouver.

    ensoite pour lancer ta deuxieme classe lorsque qu'il y a appuie sur le bouton de l'interface il te suffit de faire ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    JButton.addActionListener(new ActionListener() {
     
            public void actionPerformed(ActionEvent e) {
                    new Read()
            }
    });

  6. #6
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut
    Slt, merci pour ta réponse voila ce que j'ai ajouté :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new Read();
    }
    mais lorsque je lance mon main et que je clique sur mon boutton voici ce qu'il s'affiche :

    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
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
            at blackbox.Read.<init>(Read.java:51)
            at blackbox.Main.jButton1ActionPerformed(Main.java:106)
            at blackbox.Main.access$000(Main.java:6)
            at blackbox.Main$1.actionPerformed(Main.java:35)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6041)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5806)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4413)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2440)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    Donc ca marche pas et je sais pas pourquoi

    merci de votre aide

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

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut
    tu dit de rien mais bon c'est super sympa d'avoir pris le temps de m'aider. Car j'avais poster ce message sur un autre forum avant et personnes ne ma encore répondu à ce jour.

    Alors je pense que je vais rester ici car c'est conviviale et plein de personne sont la il y a de bon tuto aussi j'ai pas tout fait encore lol qui sait un jour peut etre je pourrais aider moi aussi les autres.

    Merci à vous

  8. #8
    Membre émérite

    Étudiant
    Inscrit en
    Octobre 2007
    Messages
    510
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Octobre 2007
    Messages : 510
    Par défaut
    mais de rien c'est le but de ce forum; aujourd'hui c'est toi qui pose des questions et dans trois mois tu commence a répondre (dans 6 tu passe tout ton temps sur le chat ) bonne chance pour la suite

  9. #9
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    82
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 82
    Par défaut
    Merci bien bonne continuation à tous aussi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 12
    Dernier message: 19/04/2008, 16h19
  2. lien entre deux sous formulaire :(
    Par souska dans le forum Access
    Réponses: 4
    Dernier message: 20/09/2005, 21h37
  3. Réponses: 5
    Dernier message: 17/08/2005, 12h40
  4. Type de lien entre deux associations
    Par thibal dans le forum PowerAMC
    Réponses: 2
    Dernier message: 17/06/2005, 16h53
  5. [VB.NET] ComboBox lien entre deux tables
    Par VDB1 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 15/07/2004, 12h15

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