1

I am trying to append a string I get from my client program. However I made it so that the append statement is in a while loop. Does this have an affect in not working in real time?

For areas like

   tfFIXMsg.append( inputLine + "\n\n\n");

and

       tfCSVLine.append(outputLine+"\n\n\n");

It will not show the message on the textArea UNTIL the while loop is done. How can I make it so that it will continue to recieve messages from client, and be able to output it onto the textArea in realtime

try {
    while ((inputLine = in.readLine()) != null) 
          { 
           System.out.println ("Server: " + inputLine); 


           tfFIXMsg.append( inputLine + "\n\n\n");


           int pos = inputLine.indexOf(inputLine, 0);
           h.addHighlight(50, 60, DefaultHighlighter.DefaultPainter);



           if (inputLine.trim().equals("Bye.")) {
               System.out.println("Exit program"); 
               break;
               } 

           Scanner input1 = new Scanner(new File(csvName));
           Scanner input2 = new Scanner(new File(csvName));
           Scanner input3 = new Scanner(new File(csvName));
           Scanner input4 = new Scanner(new File(csvName));


           String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
           outputLine = compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

           out.println(outputLine);
           tfCSVLine.append(outputLine+"\n\n\n");

           input1.close();
           input2.close();
           input3.close();
           input4.close();



          }
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
monkey doodle
  • 690
  • 5
  • 12
  • 22
  • 1
    Changes are that you're blocking the event dispatch thread (not enough context to be sure). Use [SwingWorkers](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for long running tasks. You can use the `publish()` and `process()` methods for interim results like the read strings. – kiheru Aug 07 '13 at 16:43
  • Increase the buffer size of the scanner. – Roman C Aug 07 '13 at 16:44
  • Seems like the input for the `JTextArea` is coming too fast, hence you can simply add a `Thread.sleep(...)` after calling `append()` as specified in this [thread](http://stackoverflow.com/q/17759287/1057230) :-) – nIcE cOw Aug 07 '13 at 17:28
  • 1
    For [example](http://stackoverflow.com/a/3245805/230513). – trashgod Aug 07 '13 at 18:42

2 Answers2

2

If you do things like that, they will be executed in EDT(Event Dispatch Thread). You should move your logic outside of EDT by starting a new Thread to allow Swing to repaint UI properly and post updates to UI as following:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      // Your Code Here
    }
});

Also consider use of javax.swing.SwingWorker, javax.swing.Timer and take a look at Concurrency in Swing.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
2

I guess, your code runs not on the EDT thread. If it runs, you should move that to the other one.

Then, you can use from that other thread in order to update Swing UI elements:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        tfFIXMsg.append( inputLine + "\n\n\n");
        // or whatever swing action you want to perform...
    }
});

If you need more sophisticated solutions try using SwingWorker class.

Ostap Andrusiv
  • 4,827
  • 1
  • 35
  • 38