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");
}
}
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:
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:
Check the tutorial I linked to above and the SwingWorker API for more details on this.