0
public class TextAreaAppender extends WriterAppender {

private static volatile TextArea textArea = null;
private static final Logger log = LoggerFactory.getLogger(TextAreaAppender.class);

/**
 * Set the target TextArea for the logging information to appear.
 *
 * @param textArea
 */
public static void setTextArea(final TextArea textArea) {
    TextAreaAppender.textArea = textArea;
}

/**
 * Format and then append the loggingEvent to the stored TextArea.
 *
 * @param loggingEvent
 */
@Override
public void append(final LoggingEvent loggingEvent) {
    final String message = this.layout.format(loggingEvent);
    // Append formatted message to text area using the Thread.
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            try {
                if (textArea != null) {
                    if (textArea.getText().length() == 0) {
                        Platform.runLater( () -> {
                            textArea.setText(message); 
                        });
                    } else {
                        textArea.selectEnd();
                        Platform.runLater( () -> {
                            textArea.insertText(textArea.getText().length(), message);
                        });
                    }
                }

            } catch (final Throwable t) {
                log.error("Unable to append log to text area: " + t.getMessage());
            }
            return null;
        }
    };
    new Thread(task).start();
}

That's my class. got it from http://www.rshingleton.com/javafx-log4j-textarea-log-appender/ It writes the logs that also appear at console to a textarea. The Problem is that the logs appear in a different wrong order than in the console. why? thanks in advance

jv123
  • 65
  • 7
  • You might be interested in this related question: [Most efficient way to log messages to JavaFX TextArea via threads with simple custom logging frameworks](http://stackoverflow.com/questions/24116858/most-efficient-way-to-log-messages-to-javafx-textarea-via-threads-with-simple-cu). – jewelsea Dec 08 '16 at 19:01

1 Answers1

2

You start a Task on a different Thread to post the Runnables using Platform.runLater. The Runnables are executed in the order in which they are posted, however by posting them from a new Thread you loose control over the order in which they are submitted.

I'm not really sure why you use a different thread here anyways. Just post it directly from the current thread. The code that could be "expensive" runs there anyways:

@Override
public void append(final LoggingEvent loggingEvent) {
    // create a copy to make sure it's not overwritten somewhere
    final TextArea target = textArea;

    if (target != null) {
        final String message = this.layout.format(loggingEvent);
        Platform.runLater(() -> {
             target.selectEnd();
             target.appendText(message);
        });
    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114