使用jtextfield.settext后,jtextfield不显示文本(" String");

时间:2014-05-23 07:09:07

标签: java swing serial-port jtextfield event-dispatch-thread

请帮助我解决我的问题..我知道这是一个非常小的但我无法弄清楚我的错误。这是我的代码,我将String设置为JText作为设置文本..我使用gettext方法获取文本意味着我能够设置文本。但我的问题是我的文字没有显示在文本字段中。在我的秋千表格上..当我通过阅读器刷rfid标签..我在我的控制台上也得到了所有正确的值...请帮助我如何在我的java swing应用程序中显示JTextfield中的文本... < / p>

我正在使用jtextfield12.settext(msg) 这是我的字符串,其中包含类似于... 1233445666的数据当我从串口获取的东西..我从我的字符串也得到文本也在控制台上...我的字符串不是空的..

我在串行事件method()中设置文字...请告诉我如何解决这个问题。我的代码中是否有任何错误?请帮我解决。

public class Registration extends javax.swing.JFrame implements Runnable ,SerialPortEventListener
{

    /**
     * Creates new form Registration
     */
    static CommPortIdentifier portId;
    static Enumeration portList;
    static String abc;
    private String xyz;


    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
       String fname,mname,lname,add,dob,age,pmobile,dor,foccupation,fwlocation,mothername,moccupation,mwlocation,rfidtag;

    public  Registration() 
    {
           initComponents();
          // simpleRead();

    }
    public  void simpleRead()
    {
        portList = CommPortIdentifier.getPortIdentifiers();

            while (portList.hasMoreElements()) 
            {
            portId = (CommPortIdentifier)portList.nextElement();
            System.out.println("serial " + portId.getName());
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {
                if (portId.getName().equals("COM28")) {
                    //                if (portId.getName().equals("/dev/term/a")) {
                    Registration reader = new Registration();
                    reader.simple();

                }
            }
    }
    }
    public void simple()
    {
          try 
          {
            serialPort = (SerialPort)portId.open("SimpleReadApp", 2000);
        } 
          catch (PortInUseException e) 
          {
            System.out.println(e);
        }
        try 
        {
            inputStream = serialPort.getInputStream();
        } 
        catch (IOException e) 
        {
            System.out.println(e);
        }
        try 
        {
            serialPort.addEventListener(this);
            //serialPort.getDataBits();
            // System.out.println("data bits atserial port.."+serialPort.getDataBits());
        } 
        catch (TooManyListenersException e) {
            System.out.println(e);
        }
        serialPort.notifyOnDataAvailable(true);
        try {
            serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
                                           SerialPort.STOPBITS_1,
                                           SerialPort.PARITY_NONE);
        } catch (UnsupportedCommOperationException e) {
            System.out.println(e);
        }
        readThread = new Thread(this);
        readThread.start();
    }
    @Override
    public void run() 
    {
        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            System.out.println(e);
        }
    }

    @Override
    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[40];


            try {
                while (inputStream.available() > 11) 
                {
                    int numBytes = inputStream.read(readBuffer);
                    System.out.println("numBytes::" + numBytes);
                    abc = new String(readBuffer);
                   //not getting text displaying after setting text from string...
                    jTextField12.setText(new String(readBuffer));

                    String a=jTextField12.getText().toString();
                    System.out.println("a--->"+a);
                     JOptionPane.showMessageDialog(this, "Card Read Successfull");
                    if (abc.toString() != null) 
                    {
                       System.out.println("abc value is:::" + abc);
                       //jTextField12.setText(abc);
                       //jTextField1.setText(abc);




                   }
              }
           inputStream.close();
            } catch (IOException e) {
                System.out.println(e);
            }
           break;
      }
    }

0 个答案:

没有答案