JTextArea not displaying text

前端 未结 3 1485
梦谈多话
梦谈多话 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:09

    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

    0 讨论(0)
  • 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<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.

    0 讨论(0)
  • 2021-01-19 08:19

    Instead of using setText() use append()

    0 讨论(0)
提交回复
热议问题