JTextArea not displaying text

不打扰是莪最后的温柔 提交于 2019-12-01 21:49:29

问题


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 +"\n" +jTextArea1.getText());

I am using swingworker for performing background task,here is my code

public class SaveTraffic extends SwingWorker<Void, Void> {


public GUI f = new GUI();

@Override
public Void doInBackground() throws IOException {
              //some code
              sendPacket(captor.getPacket().toString()); 
              return null;
             }//end main function

@Override
public void done() {
    System.out.println("I am DONE");

}


public void sendPacket(String Packet) {

 f.showPackets(Packet);
}

}

and the following lines of code i have written in my GUI form

 public  void showPackets(String Packet) {

 jTextArea1.append( Packet);

}

Solution: public class SaveTraffic extends SwingWorker {

     public GUI f = new GUI();

     @Override
    public Void doInBackground() throws IOException {
    f.add(jTextPane1);
   // 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<String> chunks) {
   for (String text : chunks) {
     jTextPane1.setText(text);
     f.showPackets(text);
   }
  }

  @Override
  public void done() {
   System.out.println("I am DONE");

   }

}


回答1:


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<Void, String> {

  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<String> 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.




回答2:


Instead of using setText() use append()




回答3:


Since your code snippet is just too small to give a correct answer I can think of:

  1. When you are inside the update of jTextArea, Packet is null? Can you check that.

  2. While calling this method is jTextArea has any text on it ? If none and Packet is null you wont see any result.

Edit: As per comment:

To append text use append, also read the tutorial

Though I would expect setText to show the text atleast first time, see below a bare minimum example code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class TestJTextArea {
    static void init() {
        JFrame frame = new JFrame();
        frame.setLayout(new BorderLayout());
        final JTextArea textArea = new JTextArea();
        frame.add(textArea, BorderLayout.NORTH);
        final JTextField textField = new JTextField();
        frame.add(textField,BorderLayout.CENTER);
        JButton button = new JButton("Add");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText(textField.getText());
            }
        });
        frame.add(button,BorderLayout.SOUTH);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        init();
    }
}

And with that I also agree with @Hovercraft Full Of Eels that you may have a Swing threading issue, or just use append to append text



来源:https://stackoverflow.com/questions/10674890/jtextarea-not-displaying-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!