0
votes

now this is the issue , I have an Arduino uno r3 that sends data via serial port , I have a java gui code to receive the data in bytes[] array , convert it and store in String st and display it in jTextArea . the problem is that the jtextArea doesn't display any thing , probably considering that string st value is null , but if I used the famous System.out.print(st) the result is displayed correctly in the console. i don't know whats wrong , iam posting the piece of code responsible for getting data from serial port below , the synchronized serialEvent method, any help would be greatly appreciated , help me please :) please note that jTextArea1 is declared as private in the same class and String st is declared as public in the same class thanks very much

public synchronized void serialEvent(SerialPortEvent oEvent) { 
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {           
        try {                 
                int available = input.available();
            byte[] chunk = new byte[available];
            input.read(chunk, 0, available);
            st = new String(chunk);
            jTextArea1.append(st);                                
        }catch (IOException e) {System.out.println("IO Error Occurred: " + e.toString());}
2
Since we can't see how the JTextArea is defined or used, it's impossible to provide you with an answer. Maybe you're shadowing your variables? Maybe you've not added it to the screen or added it to a component which is visible on the screen? Maybe you're blocking the Event Dispatching Thread? Maybe there are unicorns frolocking through the forest?MadProgrammer
iam using netbeans , i added the jTextArea to the frame by drag and drop , it is automatically defined as private in the same class , the JFrame is initalised and setvisible(true) , upon running the code the JFrame and the jTextArea is displayed correctly .Ahmad Abdallah
Consider providing a runnable example which demonstrates your problemMadProgrammer

2 Answers

0
votes

this is the complete JAVA code , copy pasted from NetBeans IDE , this code runs , and outputs to the console with no problems , even if I created a JButton and associate printing the string to the jTextArea under JButton click it works , but the problem is with automatically appending the string to the jTextArea within the code itself :

    import java.io.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
public class reading extends javax.swing.JFrame implements SerialPortEventListener{
    SerialPort serialPort;
    public static String st;
    public  char[] c;
    /**
     * Creates new form reading
     */
    private static final String PORT_NAMES[] = {
            "/dev/tty.usbmodem411", // Mac OS X
            "/dev/ttyUSB0", // Linux
            "COM3", // Windows
            };
    private InputStream input;
    private OutputStream output;   
    private static final int TIME_OUT = 2000;    
    private static final int DATA_RATE = 9600;
    public reading() {
        initComponents();
        initialize();
        close();    
    }
    public void initialize() {
        CommPortIdentifier portId = null;
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
        // iterate through, looking for the port
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    portId = currPortId;
                    break;
                }
            }
        }
        if (portId == null) {
                              jTextArea1.setText("scanner is not connected ! ");
        }
        try {
            serialPort = (SerialPort) portId.open(this.getClass().getName(),
                    TIME_OUT);
            // set port parameters
            serialPort.setSerialPortParams(DATA_RATE,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            input = serialPort.getInputStream();
            output = serialPort.getOutputStream();
            // add event listeners
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
        } catch (Exception e) {            
        }
    }     
    public synchronized void close() { 
       if (serialPort != null) {
           serialPort.removeEventListener();
           serialPort.close();
        }
       }    
    public synchronized void serialEvent(SerialPortEvent oEvent) {       
        if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {           
            try {                 
                    int available = input.available();
                byte[] chunk = new byte[available];
                input.read(chunk, 0, available);
                st = new String(chunk);
                jTextArea1.append(st);
                System.out.print(st);
                try{
                Thread.sleep(5000);                 
} catch(InterruptedException ex) {
    Thread.currentThread().interrupt();
}                                                                                                                                                                                           
                // Displayed results are codepage dependent                                                                                                                                                                                                                                       
            }
catch (IOException e) {
System.out.println("IO Error Occurred: " + e.toString());

}          
        }
       // Ignore all the other eventTypes, but you should consider the other ones.
    }        
    /**
     * 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">                                                                                             
    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        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(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(211, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(23, 23, 23))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(179, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(reading.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
reading main = new reading();
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new reading().setVisible(true);
                main.initialize();
            }
        });
    }      
    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   

    }
0
votes

Ok I got it ! I had to create a jPanel at first inside the JFrame and add all the components into it instead of adding the components inside the JFrame directly ! now it works like a charm ! thank you very much for your concern .