-1

below my code i code wright for gps is give me coninue data on 4800 baud rate this data display continue by "System.out.println(st)" but same data not dispaly in a.setText(st) where a is Textbox variable. some know how to my textbox update like System.out.println line.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.UnsupportedCommOperationException;
import javax.comm.*;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * test.java
 *
 * Created on Mar 11, 2013, 9:08:52 AM
 */
/**
 *
 * @author DJ ROCKS
 */
public class test extends javax.swing.JFrame {
    public boolean bp=true;

    /** Creates new form test */
    public test() {
        initComponents();
    }

    /** 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() {

        ok = new javax.swing.JButton();
        a = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        ok.setText("ok");
        ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okActionPerformed(evt);
            }
        });

        a.setText(" ");
        a.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(62, 62, 62)
                        .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, 394, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(238, 238, 238)
                        .addComponent(ok)))
                .addContainerGap(124, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(a, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(67, 67, 67)
                .addComponent(ok)
                .addContainerGap(160, Short.MAX_VALUE))
        );

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

    private void okActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:

        if(evt.getSource()==ok)
        {
            CommPortIdentifier portId = null; 
String wantedPortName="COM16";

 Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers();


while(portIdentifiers.hasMoreElements())
{
    CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement();
    if(pid.getPortType() == CommPortIdentifier.PORT_SERIAL &&
       pid.getName().equals(wantedPortName)) 
    {
        portId = pid;
        break;
    }
}
if(portId == null)
{
    System.err.println("Could not find serial port " + wantedPortName);
    System.exit(1);
}
else
{
    System.out.println("system find gps reciever");
}
SerialPort port = null;
try {
    port = (SerialPort) portId.open(
        "RMC", 
        1);
    System.out.println("all are ok"); 
} catch(PortInUseException e) {
    System.err.println("Port already in use: " + e);
    System.exit(1);
}
            try {
                //int i=Integer.parseInt(new vps().baud_rate.getItemAt(new vps().baud_rate.getItemCount()));
               port.setSerialPortParams(
                   4800,
                   SerialPort.DATABITS_8,
                   SerialPort.STOPBITS_1,
                   SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException ex) {
                Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
            }



BufferedReader is = null;  


try {
  is = new BufferedReader(new InputStreamReader(port.getInputStream()));
  System.out.println("data is ok");
} catch (IOException e) {
  System.err.println("Can't open input stream: write-only");
  is = null;
}



 //this is variable is ouside of class and define by public it work 
    while(true)
    {

String st = null;
                try {
                    st = is.readLine();
                } catch (IOException ex) {
                    Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
                }


       System.out.println(st);
        //st = st.replace(st, "");
      a.setText(st);
      try
      {

          Thread.sleep(1000);
      }
      catch(InterruptedException ie)
      {
          System.out.printf(ie.getMessage());
      }
}
/*if (is != null) try {
                is.close();
            } catch (IOException ex) {
                Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
            }

if (port != null) port.close();
*/
}



    }                                  

    private void aActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField a;
    private javax.swing.JButton ok;
    // End of variables declaration
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
dharma11
  • 9
  • 1
  • 1
    What is a "textbox"? I don't know any component by that name. – camickr Mar 12 '13 at 19:04
  • 4
    Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Mar 13 '13 at 00:44
  • when i am not used Thread.sleep() that time also same problem occurred. – dharma11 Mar 13 '13 at 03:23
  • i don't know about EDT can you Explain further about it – dharma11 Mar 13 '13 at 03:30

1 Answers1

0

It seems as though you may be trying to rewrite the same String. I would try to call a "get" method that returns a String instead of referring to the variable "st". I do this to avoid a situations in which the program will try to rewrite the String since Strings are immutable.

I would try something like this:

private String getString()
{
try {
    String st = new String(""+is.ReadLine());
    return st;
    } catch (IOException ex) {
                Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}

Use this to refer to the new String from BufferedReader "is":

   a.setText(getString());

Also, it looks as if you have a code block within comment lines here:

/*if (is != null) try {
            is.close();
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
      if (port != null) port.close();
}*/

Please correct me if I'm wrong here.

ibtokin
  • 357
  • 3
  • 12
  • my friend i already applied this logic but this logic give me single string in Textbox but i want to continue string using loop. – dharma11 Mar 13 '13 at 03:32
  • Have you tried updating it on the UI thread? Oops, this is Java, not Android, disregard. – ibtokin Mar 15 '13 at 13:14