Remplir Combobox sous netbeans

Résolu
Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008 - 19 mai 2008 à 15:02
bricolomi Messages postés 23 Date d'inscription dimanche 15 juin 2003 Statut Membre Dernière intervention 25 juin 2010 - 31 déc. 2008 à 18:21
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

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 :
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.(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

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

Bretzel : Tout est be qui finit bien!!

2 réponses

Bouboukick Messages postés 78 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 14 octobre 2008
19 mai 2008 à 16:23
C'est bon j'ai trouvé mon erreur voici ce que j'ai changé et cela fonctionne du tonner de feu lol

    public Main()
    {
        initComponents();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements())
        {
        portId = (CommPortIdentifier) portList.nextElement();
            jComboBox1.addItem(portId.getName());
        }
    }

Merci bien

Bretzel : Tout est be qui finit bien!!
3
bricolomi Messages postés 23 Date d'inscription dimanche 15 juin 2003 Statut Membre Dernière intervention 25 juin 2010
31 déc. 2008 à 18:21
Bonjour
Savez-vous comment faire fonctionner sous windows avez RXTXcomm
merci

dede
0
Rejoignez-nous