JTextArea does not display text

In my function for displaying text in textarea, I wrote the following lines of code, but does not display text

jTextArea1.setText( Packet +"\n" +jTextArea1.getText()); 

I use swingworker to do a 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 are written in my GUI form

  public void showPackets(String Packet) { jTextArea1.append( Packet); } 

Solution: Open SaveTraffic class 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"); } 

}

+4
source share
3 answers

You have a very incomplete question, one without enough information to give an answer, and one that makes us guess, but based on this line in your original message:

The function is called continuously ...

I guess, and I bet the money that you have a problem with Swing threads. You probably want to read and use SwingWorker.

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

Yes, your question is the Swing concurrency issue caused by your Swing calls from the background thread. To avoid this, you need to export data from doInBackground and call it in the Swing event stream. One way to do this is through a couple of publish / process method:

 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 out the tutorial I linked above and the SwingWorker API for more details on this.

+5
source

Instead of using setText () use append ()

+6
source

Since your piece of code is too small to give the correct answer, I can think:

  • When you are inside jTextArea update, is Packet null? You can check it out.

  • When calling this jTextArea method jTextArea is there text on it? If none and Packet are null, you will not see any result.

Edit: According to the comment:

To add text, use append , also read tutorial

Although I would expect setText display text at least for the first time, see below for an example minimum code example:

 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 might have a problem with Swing threads, or just use append to add text

+2
source

Source: https://habr.com/ru/post/1413406/


All Articles