Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write logs and display them real-time in a Java Swing application?

I have created a GUI program that uses a class to perform some tasks.

I would like to add logging in both the JFrame and the class. Logs should be kept in a file and displayed in a JTextArea concurrently.

What is a convient solution for this?

like image 911
GorillaApe Avatar asked Mar 19 '26 05:03

GorillaApe


2 Answers

Create a wrapper that

  1. updates your JTextArea
  2. logs via log4j, SLF4J or Apache Commons Logging or another logging framework
public void log(String msg) {
      appendToJTextArea(msg);
      LOG.info(msg);
 }
like image 105
dertkw Avatar answered Mar 21 '26 17:03

dertkw


I suggest using a simple System.out.println() call and then running your application like this:

java -cp path/to/my/class/or/jar/MyClass.class MyClass -debug > myLog.log

Which will create a text pane for you (-debug parameter), as well as put them in a log file for you.

like image 22
Richard J. Ross III Avatar answered Mar 21 '26 19:03

Richard J. Ross III



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!