Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6957825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:03:37+00:00 2026-05-27T15:03:37+00:00

I’m currently trying to make Log4J log into a JTextPane . I want to

  • 0

I’m currently trying to make Log4J log into a JTextPane. I want to use a TextPane because I want basic highlighting (e.g. errors are red and infos are green).

I have two loggers set up, one (the root-logger) logs everything into a file and the other (guiLogger) logs only some errors and infos on the GUI in a JTextPane.

The problem I’m currently facing is, that I can’t get appending to the TextPane to work. What I currently have looks something like this:

public class Log extends AppenderSkeleton{

    private final JTextPane log;
    private final StyledDocument doc;

    public Log(){
        super();
        log = new JTextPane();
        doc = log.getStyledDocument();
    }

    @Override
    protected void append(LoggingEvent loggingEvent) {
        try {
            doc.insertString(doc.getLength(), "Hello World!", null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }

    public JTextPane getView(){
        return log;
    }
}

The Log4J config-file looks like this:

# The root-logger should log everything.
log4j.rootLogger = DEBUG, file

# Append the logs to a file.
log4j.appender.file = org.apache.log4j.RollingFileAppender
# [...]

# The logger which logs on the GUI (just some user-information).
log4j.logger.guiLogger = INFO, gui

# Append the logs to the GUI
log4j.appender.gui = mypackage.Log
# Formatting of the output:
log4j.appender.gui.layout = org.apache.log4j.PatternLayout
log4j.appender.gui.layout.ConversionPattern = %m%n

The append()-method gets called, and the insertString()-method performs clean (it does not enter the catch-block), but I don’t see any content in the TextPane on the GUI.

What I have tried to fix this:

  • Execute the insertString()-method using SwingUtilities.invokeLater()
  • Execute the insertString()-method from a SwingWorker
  • Methods like validate(), revalidate() and repaint() on the JTextPane
  • Not using the global StyledDocument-object but directly getting it from the log-instance: log.getStyledDocument().insertString(0, "Hello World!", info_log);
  • The setText()-method of the JTextPane (only works in the constructor).

Since the JTextPane has no fireContentChanged()-method (or similar), I’m kind of lost here.


I have played around a bit more and found some other things:

  • The StyledDocument gets updated (calling getText() shows that the text has been inserted).
  • When I call the append() or insertString()-method directly from the compiler (after initializing the StyledDocument and the JTextPane), it all works fine.

Also, I checked which thread called the method by adding this to the append()-method body:

System.out.println("Thread: "+Thread.currentThread().getName());

It shows the following if I simply do two log-statements from somewhere in the code:

Thread: AWT-EventQueue-0
Thread: AWT-EventQueue-0

and when I call the append()-method directly from the constructor of the Log-class (plus the two logging-statements from above), it shows the following:

Thread: AWT-EventQueue-0
Thread: AWT-EventQueue-0
Thread: AWT-EventQueue-0

The first call appends the text probably. But the other two don’t seam to work.

My GUI is build from the AWT-EventQueue by using SwingUtilities.invokeLater(). The two logging-calls are made in the same context (and therefore come from the EventQueue, too).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:03:38+00:00Added an answer on May 27, 2026 at 3:03 pm

    The text pane is private to the appender, and you don’t have any getter for it. So I would guess that the GUI has a text pane, and the logger has another text pane it appends to.

    That, or you’re getting the text pane from a Log instance that is not the same as the Log instance that Log4j instantiates.

    Also, the appender might be use by several threads, but the Swing components may only be accessed from the event dispatch thread. Every append to the text pane should be done inside a SwingUtilities.invokeLater() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.