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

NetBeans Java Discussion :

Remplir une Combobox


Sujet :

NetBeans 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 Remplir une Combobox
    Bonjour j'ai un code qui se connecte a mon port com et me fournit aussi tout les port disponible sur mon pc.

    Le souci est que j'aimerais pouvoir mettre ma liste des ports disponible dans une combobox.

    Voici mes tests effecué.

    1er test : je met juste un jComboBox1.addItem(portId.getName());et j'obtient juste mon dernier port

    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
    package blackbox;
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
     
    public class Frame extends javax.swing.JFrame
    {
        static CommPortIdentifier portId;
        static Enumeration          portList;
        InputStream              inputStream;
        SerialPort              serialPort;
        Thread              readThread;
        private List list;
     
        public Frame()
        {
            this.list = new ArrayList();
            initComponents();
            jComboBox1.addItem(portId.getName());
     
     
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jComboBox1 = new javax.swing.JComboBox();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            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()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(179, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(264, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
        public static void main(String args[])
        {
            boolean              portFound = false;
            String              defaultPort = "/dev/ttyS0";
     
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Frame().setVisible(true);
                }
            });
            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;
                //Main reader = new Main();
            }
            }
     
        }
        if (!portFound)
            {
            System.out.println("port " + defaultPort + " not found.");
        }
        }
        // Variables declaration - do not modify
        private javax.swing.JComboBox jComboBox1;
        // End of variables declaration
        class Main implements Runnable, SerialPortEventListener
        {
     
            public Main()
        {
        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;
        }
        }
     
        }
    }
    2eme test : je met une boucle while et la j'ai un message d'erreur suivant :
    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
    Compiling 1 source file to NetBeansProjects/BlackBox/build/classes
    Found port: /dev/ttyS0
    Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
            at java.util.Arrays.copyOf(Arrays.java:2760)
            at java.util.Arrays.copyOf(Arrays.java:2734)
            at java.util.Vector.ensureCapacityHelper(Vector.java:226)
            at java.util.Vector.addElement(Vector.java:573)
            at javax.swing.DefaultComboBoxModel.addElement(DefaultComboBoxModel.java:123)
            at javax.swing.JComboBox.addItem(JComboBox.java:698)
            at blackbox.Frame.<init>(Frame.java:22)
            at blackbox.Frame$1.run(Frame.java:62)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
            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)
    voici le 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
    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
    package blackbox;
    import java.io.*;
    import java.util.*;
    import javax.comm.*;
     
    public class Frame extends javax.swing.JFrame
    {
        static CommPortIdentifier portId;
        static Enumeration          portList;
        InputStream              inputStream;
        SerialPort              serialPort;
        Thread              readThread;
        private List list;
     
        public Frame()
        {
            this.list = new ArrayList();
            initComponents();
            portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements())
            {
            jComboBox1.addItem(portId.getName());
            }
     
        }
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
     
            jComboBox1 = new javax.swing.JComboBox();
     
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
     
            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()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 209, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(179, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(264, Short.MAX_VALUE))
            );
     
            pack();
        }// </editor-fold>
        public static void main(String args[])
        {
            boolean              portFound = false;
            String              defaultPort = "/dev/ttyS0";
     
            java.awt.EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    new Frame().setVisible(true);
                }
            });
            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;
            }
            }
     
        }
        if (!portFound)
            {
            System.out.println("port " + defaultPort + " not found.");
        }
        }
        // Variables declaration - do not modify
        private javax.swing.JComboBox jComboBox1;
        // End of variables declaration
        class Main implements Runnable, SerialPortEventListener
        {
     
            public Main()
        {
        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 pour votre aide

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    230
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 230
    Par défaut
    Combien d'éléments as-tu dans ta combobox ?
    Met un compteur sur la ligne combo.addItem pour savoir ?

  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
    le nombre est variable selon les PCs.

    Sur mon PC il y en a 3 mais de toute façons j'ai mis une combobox infini comme cela pas de problème de taille

  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
    C'est bon j'ai réussi à afficher dans ma combobox tout les ports disponible sur mon pc voi ci ce que j'ai changé :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public Main()
    {
            initComponents();
            portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) 
            {
    	    portId = (CommPortIdentifier) portList.nextElement();
                jComboBox1.addItem(portId.getName());
            }
    }
    Voila merci bien

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

Discussions similaires

  1. [Spread designer] Remplir une combobox
    Par valoji dans le forum Bases de données
    Réponses: 1
    Dernier message: 31/03/2006, 11h32
  2. [VBA-E] Comment remplir une ComboBox ?
    Par yaya54 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 07/03/2006, 09h03
  3. [C#] Remplir une combobox avec le champs d'une table Access
    Par Damsou dans le forum Windows Forms
    Réponses: 4
    Dernier message: 23/06/2005, 15h31
  4. [VB.NET] Remplir une ComboBox avec une requete sql
    Par graphicsxp dans le forum Windows Forms
    Réponses: 6
    Dernier message: 22/03/2005, 15h29
  5. Réponses: 2
    Dernier message: 26/07/2004, 13h34

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