I recently wanted to redirect the output of Log4j to a JTextPane, so as to output messages to the JTextPane using a different color depending on the severity.

I spent some time searching for information and I quickly put together a small example. The following screenshot shows the output.

screenshot.jpg

The source code is available here.

2 thoughts on “Log4j result in JTextPane

  1. Hi,
    Thanks a lot for this example,very useful

    but i need more more enhancement,when you are writing any of the log i.e warn ,error to textpane but log should not one liner than I want when i print log to textpane ,one by one line should be print,but here all logs are printed at the end.

    to generate above scenario add a for loop in any of the button’s actionlistener with thread sleep.

    exmaple

    jButton2.addActionListener(new java.awt.event.ActionListener(){
    public void actionPerformed(java.awt.event.ActionEvent e)
    {
    for(int i=1; i<3;i++)
    {
    MainLogger.logWarn("This is a warning!!");
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e1) {
    e1.printStackTrace();
    }
    }

    Reply

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url=""> 

required

Page last modified: 23:16 on November 8, 2013 (UTC+2)