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

  • SEARCH
  • Home
  • 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 6209477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:59:29+00:00 2026-05-24T05:59:29+00:00

Is there any simple way to parse Ansi colors in log files, and use

  • 0

Is there any simple way to parse Ansi colors in log files, and use it in text fields in Swing (JTextArea, JTextPAne,…)?

  • 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-24T05:59:30+00:00Added an answer on May 24, 2026 at 5:59 am

    Not tried it, but there’s some code here (which needs some formatting to look nice) which claims to be an ANSI colored JTextPane subclass…


    For posterity, here is the class run through NetBeans to format the code

    import javax.swing.*;
    import javax.swing.text.*;
    import java.awt.Color;
    
    public class ColorPane extends JTextPane {
      static final Color D_Black   = Color.getHSBColor( 0.000f, 0.000f, 0.000f );
      static final Color D_Red     = Color.getHSBColor( 0.000f, 1.000f, 0.502f );
      static final Color D_Blue    = Color.getHSBColor( 0.667f, 1.000f, 0.502f );
      static final Color D_Magenta = Color.getHSBColor( 0.833f, 1.000f, 0.502f );
      static final Color D_Green   = Color.getHSBColor( 0.333f, 1.000f, 0.502f );
      static final Color D_Yellow  = Color.getHSBColor( 0.167f, 1.000f, 0.502f );
      static final Color D_Cyan    = Color.getHSBColor( 0.500f, 1.000f, 0.502f );
      static final Color D_White   = Color.getHSBColor( 0.000f, 0.000f, 0.753f );
      static final Color B_Black   = Color.getHSBColor( 0.000f, 0.000f, 0.502f );
      static final Color B_Red     = Color.getHSBColor( 0.000f, 1.000f, 1.000f );
      static final Color B_Blue    = Color.getHSBColor( 0.667f, 1.000f, 1.000f );
      static final Color B_Magenta = Color.getHSBColor( 0.833f, 1.000f, 1.000f );
      static final Color B_Green   = Color.getHSBColor( 0.333f, 1.000f, 1.000f );
      static final Color B_Yellow  = Color.getHSBColor( 0.167f, 1.000f, 1.000f );
      static final Color B_Cyan    = Color.getHSBColor( 0.500f, 1.000f, 1.000f );
      static final Color B_White   = Color.getHSBColor( 0.000f, 0.000f, 1.000f );
      static final Color cReset    = Color.getHSBColor( 0.000f, 0.000f, 1.000f );
      static Color colorCurrent    = cReset;
      String remaining = "";
    
      public void append(Color c, String s) {
        StyleContext sc = StyleContext.getDefaultStyleContext();
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
        int len = getDocument().getLength(); // same value as getText().length();
        setCaretPosition(len);  // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
      }
    
      public void appendANSI(String s) { // convert ANSI color codes first
        int aPos = 0;   // current char position in addString
        int aIndex = 0; // index of next Escape sequence
        int mIndex = 0; // index of "m" terminating Escape sequence
        String tmpString = "";
        boolean stillSearching = true; // true until no more Escape sequences
        String addString = remaining + s;
        remaining = "";
    
        if (addString.length() > 0) {
          aIndex = addString.indexOf("\u001B"); // find first escape
          if (aIndex == -1) { // no escape/color change in this string, so just send it with current color
            append(colorCurrent,addString);
            return;
          }
    // otherwise There is an escape character in the string, so we must process it
    
          if (aIndex > 0) { // Escape is not first char, so send text up to first escape
            tmpString = addString.substring(0,aIndex);
            append(colorCurrent, tmpString);
            aPos = aIndex;
          }
    // aPos is now at the beginning of the first escape sequence
    
          stillSearching = true;
          while (stillSearching) {
            mIndex = addString.indexOf("m",aPos); // find the end of the escape sequence
            if (mIndex < 0) { // the buffer ends halfway through the ansi string!
              remaining = addString.substring(aPos,addString.length());
              stillSearching = false;
              continue;
            }
            else {
              tmpString = addString.substring(aPos,mIndex+1);
              colorCurrent = getANSIColor(tmpString);
            }
            aPos = mIndex + 1;
    // now we have the color, send text that is in that color (up to next escape)
    
            aIndex = addString.indexOf("\u001B", aPos);
    
            if (aIndex == -1) { // if that was the last sequence of the input, send remaining text
              tmpString = addString.substring(aPos,addString.length());
              append(colorCurrent, tmpString);
              stillSearching = false;
              continue; // jump out of loop early, as the whole string has been sent now
            }
    
            // there is another escape sequence, so send part of the string and prepare for the next
            tmpString = addString.substring(aPos,aIndex);
            aPos = aIndex;
            append(colorCurrent, tmpString);
    
          } // while there's text in the input buffer
        }
      }
    
      public Color getANSIColor(String ANSIColor) {
        if (ANSIColor.equals("\u001B[30m"))        { return D_Black; }
        else if (ANSIColor.equals("\u001B[31m"))   { return D_Red; }
        else if (ANSIColor.equals("\u001B[32m"))   { return D_Green; }
        else if (ANSIColor.equals("\u001B[33m"))   { return D_Yellow; }
        else if (ANSIColor.equals("\u001B[34m"))   { return D_Blue; }
        else if (ANSIColor.equals("\u001B[35m"))   { return D_Magenta; }
        else if (ANSIColor.equals("\u001B[36m"))   { return D_Cyan; }
        else if (ANSIColor.equals("\u001B[37m"))   { return D_White; }
        else if (ANSIColor.equals("\u001B[0;30m")) { return D_Black; }
        else if (ANSIColor.equals("\u001B[0;31m")) { return D_Red; }
        else if (ANSIColor.equals("\u001B[0;32m")) { return D_Green; }
        else if (ANSIColor.equals("\u001B[0;33m")) { return D_Yellow; }
        else if (ANSIColor.equals("\u001B[0;34m")) { return D_Blue; }
        else if (ANSIColor.equals("\u001B[0;35m")) { return D_Magenta; }
        else if (ANSIColor.equals("\u001B[0;36m")) { return D_Cyan; }
        else if (ANSIColor.equals("\u001B[0;37m")) { return D_White; }
        else if (ANSIColor.equals("\u001B[1;30m")) { return B_Black; }
        else if (ANSIColor.equals("\u001B[1;31m")) { return B_Red; }
        else if (ANSIColor.equals("\u001B[1;32m")) { return B_Green; }
        else if (ANSIColor.equals("\u001B[1;33m")) { return B_Yellow; }
        else if (ANSIColor.equals("\u001B[1;34m")) { return B_Blue; }
        else if (ANSIColor.equals("\u001B[1;35m")) { return B_Magenta; }
        else if (ANSIColor.equals("\u001B[1;36m")) { return B_Cyan; }
        else if (ANSIColor.equals("\u001B[1;37m")) { return B_White; }
        else if (ANSIColor.equals("\u001B[0m"))    { return cReset; }
        else { return B_White; }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any simple way to create instance of modal DialogBox with single text
Is there a simple way I could use any subclass of Lucene's Analyzer to
Is there any simple way to programatically colorize images in .NET? Basically we have
Is there any simple way to generate a default crud (given an entity) with
Is there any (simple) way to get some control of the order in which
Is there any simple way to tell if UNC path points to a local
In git, is there any (simple) way to modify the index so that only
is there any other simple,nicer way? require 'pp' a1 = [02/28/10,Webinars,131,0,26 Feb 2010,0,3d, 8h,
Is there any way to keep this simple JQuery animation from flashing? http://jsfiddle.net/v3DVf/6/
Is there any way of setting up a simple bash script such as chmod

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.