Java log ==> JTextArea

Necessity: Output Java application log to GUI component, for example, JTextArea.

Concern: It is necessary to record things from any class, statically. However, the logger GUI component does not have to be static (obviously), since it is a member of the parent component.

What should I do?

+5
source share
4 answers

Create a syslog provider and add a "text box" to it.

Singleton logger example:

interface Listener {
    void log(String log);
}

enum Logger {

    instance;

    private List<Listener> listeners = new LinkedList<Listener>();

    public void addListener(Listener l) {
    synchronized(listeners) {
        listeners.add(l);
     }
    }

    public void log(String log) {
        synchronized(listeners) {
            for(Listener l : listeners)
                l.log(log);
        }
    }
}

Add your listener (which you need to implement yourself) as follows:

Logger.instance.addListener(myTextField);

And use it (from any class) as follows:

Logger.instance.log("Hello World!");

, log4j.

+2

, . , log4j XML-, .

:. .

+2

. , , . SWT JFace Databinding, , SWING .

- , , Arraylist of Strings (logentries). GUI , . ( ), .

+1

, .

My Swing 9 (, , , , , , , )

:

view -> logger, controller, utils, model
controller -> logger, application, model, utils
application -> service, model, utils, platform
service -> persistence, model, utils
platform -> model
utils -> no dependencies
model -> no dependencies
logger -> model, utils

, .

, ( ), (, JFrame, JFace = ).

(, ) , .

But I think the data block works the same way. I assume you are structure dependent. My solution is wrong, so if I want to switch from Swing to swt, don’t worry, I just implement it for swt and my business logic remains intact.

You should all think that design is bigger. (And use maven.)

+1
source

All Articles