Switch to full style
Graphics and animation Java code examples
Post a reply

GUI logger

Sat Nov 27, 2010 10:58 pm

The following class is simple idea which you can have a text area to log events in your desktop application , as you may notice that the textArea ref is final and static , so we will have one textarea all over the application process.

Code:


import java
.util.Date;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class GUILogger {

    private static GUILogger logger = null;
    private static final JTextArea textArea = new JTextArea(7, 50);
    private static final JScrollPane TextAreaScroll = new JScrollPane
(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    private GUILogger() {
    }

    public static GUILogger getInstance() {
        if (logger == null) {
            logger = new GUILogger();
        }
        return logger;
    }

    public void logInfo(String info) {
        textArea.append("\n" + new Date() + ": " + info);
    }

    public static GUILogger getLogger() {
        return logger;
    }

    public static void setLogger(GUILogger logger) {
        GUILogger.logger = logger;
    }

    public static JScrollPane getTextAreaScroll() {
        return TextAreaScroll;
    }

    public static JTextArea getTextArea() {
        return textArea;
    }
}
 


Add it to a panel .
Code:

import java
.awt.BorderLayout;
import javax.swing.JPanel;

public class 
LogPanel extends JPanel {

    private 
GUILogger gUILogger GUILogger.getInstance();

    public 
LogPanel() {

        
add(gUILogger.getTextAreaScroll());
    }
}
 




Post a reply
  Related Posts  to : GUI logger
 Asynchronous Logger using Java     -  
 Java extending org.apache.log4j.Logger     -