Search code examples
javaswingjtextarea

Set a JTextArea to act like a log


Hello how could I make it so that the JTextArea (log) act as a log and display the text that is input and output?

Here is what I have so far.

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String mess = message.getText();
    System.out.println(mess);

}

Here are the vars

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton3;
private javax.swing.JButton jButton4;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea log;
private javax.swing.JTextField message;
// End of variables declaration     

Thanks in advance for any help!


Solution

  • append() will append the data to the end.

    Instead, you need to first get the text using the getText(), concatenate the log to it, then use setText() with the newly formed error message.

    So, conceptually, your new text is :

    error log + previously displayed text

    SSCCE

    package stack;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Date;
    import java.util.Random;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;
    
    public class AppendMyError {
        public static void main(String[] args) {
            final String[] errorMsg = {"NullPointerException",
                                       "ArrayIndexOutOfBoundsException",
                                       "OutOfMemoryException"
                                      };
    
    
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run(){
                    final JFrame jFrame = new JFrame("Useless Title");
                    final JButton errorBtn = new JButton("Click");
                    final JTextArea errorLog = new JTextArea(30,30);
                    final JScrollPane scroll = new JScrollPane(errorLog);
                    final Random rand = new Random();
    
                    jFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
                    jFrame.getContentPane().add(scroll);
                    jFrame.getContentPane().add(errorBtn);
                    jFrame.pack();
                    jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    jFrame.setVisible(true);
    
                    errorBtn.addActionListener(new ActionListener(){
                        @Override
                        public void actionPerformed(ActionEvent event) {
                            String currentText = errorLog.getText();
                            String newError = new Date() + " " +  errorMsg[rand.nextInt(errorMsg.length)];
                            String newTextToAppend = newError + "\n" + currentText;
                            errorLog.setText(newTextToAppend);
                        }
                    });
    
                }
            });
    
        }
    }  
    

    Output:
    enter image description here

    If you look at the time, the newer messages are at the top.