JTextArea not displaying text

前端 未结 3 1522
梦谈多话
梦谈多话 2021-01-19 07:31

In my function for displaying text in textarea,i have written following lines of code but it is not displaying any text

        jTextArea1.setText( Packet +\         


        
3条回答
  •  悲哀的现实
    2021-01-19 08:10

    Yours is a very incomplete question, one without enough information to allow an answer, and one that forces us to guess, but based on this line in your original post:

    The function is called continuously ...

    I'll guess, and I will bet money that you've got a Swing threading issue. You will probably want to read up on and use a SwingWorker.

    Start here to learn about the EDT and SwingWorkers: Concurrency in Swing.

    Yes, yours is a Swing concurrency issue caused by your making Swing calls from within the background thread. To avoid doing this, you need to export the data from doInBackground and call it on the Swing event thread. One way to to do this is via the publish/process method pair:

    public class SaveTraffic extends SwingWorker {
    
      public GUI f = new GUI();
    
      @Override
      public Void doInBackground() throws IOException {
    
         // some code
    
         publish(captor.getPacket().toString());
    
         // the method below is calling sendPacket on the background thread
         // which then calls showPackets on the background thread
         // which then appends text into the JTextArea on the background thread
         //sendPacket(captor.getPacket().toString());
    
         return null;
      }
    
      @Override
      protected void process(List packetTextList) {
         for (String packetText : packetTextList) {
            sendPacket(packetText); //edit, changed to match your code
         }
      }
    
      @Override
      public void done() {
         System.out.println("I am DONE");
    
      }
    
      public void sendPacket(String Packet) {
    
         f.showPackets(Packet);
      }
    }
    

    Check the tutorial I linked to above and the SwingWorker API for more details on this.

提交回复
热议问题