Java Tips Weblog

  • Blog Stats

    • 1,199,104 hits
  • Categories

  • Archives

Message Console

Posted by Rob Camick on November 8, 2008

There may be times when you want to capture output from your program and display it for the user. This is generally done by creating a console. Using Swing it is not too difficult to create a simple console using a JTextArea or JTextPane. Our message console will be able to display output written to System.out and System.err.

The MessageConsole class will enable you to use a text area or text pane as a simple console. First you must decide which component to use:

  • JTextArea – will be more efficient
  • JTextPane – will allow you to color the text from each source

Next you must decide how you want the console to function:

  • append – messages will be added to the bottom of the console
  • insert – messages will be inserted as the first line of the console

Finally, you need to decide if you need to limit the number of lines contained in the console. The MessageConsole will use my LimitLinesDocumentListener described in an earlier blog entry.

So the code for a simple useage of this class might be something like:


somePanel.add( new JScrollPane( textComponent ) );
MessageConsole mc = new MessageConsole(textComponent);
mc.redirectOut();
mc.redirectErr(Color.RED, null);
mc.setMessageLines(100);

message-console

One last comment. You can optionally redirect the message to a PrintStream. So if you did something like the following:

mc.redirectOut(null, System.out);

The message would display in your message console as well as the normal output console.

Get The Code

MessageConsole.java

See Also

Limit Lines in Document

About these ads

26 Responses to “Message Console”

  1. Keith Corlett said

    Your blog is just full of useful suprises. Thanks for the fish!

    Cheers. Keith.

  2. Hi,

    thanks for sharing this wonderfull code, but i was wondering if it was possible to embed this in sort of dropdownbox ( saw this on the lotus notes console) were u see 1 line at a time and when opening you spot fe te 10 last lines, did u ever make this?

    with regards
    Sven

  3. rniamo said

    which licence ?

  4. Kylex said

    How do you deal with the synchronization needed?
    I didn’t test your solution but as far as I looked into your code, the Document.remove is not in synch with your JTextArea.setCaretPosition, that could cause a problem since Document.getLength() could return a value X and just after that the length of the document is modified by your LimitLineDocumentListener so the actual value would be Y = X – Z . Whereas JTextArea.setCaretPosition(X) is of course not valid anymore.

    Did I overlook sth. here or is this a problem of your current solution?
    Could ofcourse be easily fixed with a lock on the document which both share.

    Beside of that: Looks good & useful!

  5. Very useful thanks for posting this. I found a few other snippits of code online but none that worked as well as yours across multiple classes and it was easy to integrate into my program :)

  6. brunez said

    Thanks for this, it works fine.

    But, is it possible to print the info in the console while the application is busy?
    I’ve written a program that searches files, and I’d like to print the completed percentage while it’s at it, but it only prints the information when it’s done, which is obviously not very useful.

    Is there any way to have it print info during the task?

  7. Thomas said

    I’m having some issues with the way I’ve implemented this code.

    I added the MessageConsole class to my project and initialized it in the EDT. I have several background tasks which output messages just fine, but if I use System.out within the main thread the text will fail to update in the TextPane. I’m guessing the EDT is blocking it, but I’m not sure how to fix it. Run all messages from the main thread as tasks, even if it’s just to send a message to the TextPane?

    Thanks,
    Tom

    • Rob Camick said

      I don’t understand why you would have problems using System.out.println() in the main thread. Maybe if you post an SSCCE by using the “Contact Us” page I can see what is happening.

      • Thomas said

        I figured it out. It has to do with the fact that I am using my own spacing (sometimes I would want a \n to separate textual output). Running a method twice that has the following code:

        System.out.println(“This will work!”);
        System.out.println(“This doesn’t work!” + “\n”);

        would print:

        This will work!
        This doesn’t work!

        This will work!

        Whereas running a method twice with the order reversed:

        System.out.println(“This doesn’t work!” + “\n”);
        System.out.println(“This will work!”);

        would print:

        This doesn’t work!

        This will work!
        This doesn’t work!

        This will work!

        So, as in the first example, it seems the \n is holding up the buffer. Nothing that can’t be fixed with a little modification to the MessageConsole code, but it was something I initially overlooked.

      • Greg M. said

        Hi Tom,

        I use MessageConsole and have some problem with a MessageConsole and a background task that write on System.out/err : the application block when write some line.

        > Nothing that can’t be fixed with a little modification to the MessageConsole code

        Perhaps your fix on MessageConsole could help me ?

      • Rob Camick said

        I just fixed a problem that occurred when the message contained a “\n” at the end of the message (as noted by Thomas). In these cases the message was just stored in the buffer and never added to the text pane.

      • Greg M. said

        Thanks for the reply.

        I try your updated code, but it’s the same problem.
        It seems it’s the LimitLinesDocumentListener that blocks (when I remove setMessageLines(…) option, it work well).

  8. Jeremy said

    This is fantastic. The code certainly provided me with the information I need to implement my processing display logic. GUI has never been my strong suite but this code is very clear, well written, and well commented. Thank you for providing this code and explanation!

  9. teo said

    thank you thank you thank you very much!

  10. Anonymous said

    Thanks a lot!!

  11. Anonymous said

    Hi,

    Can you please tell me how we can print the log4j logs with the same code which you have provided?

    Right now I can see only the System.out and System.err are getting printed in the textarea but not the log4j logs. Any help?

    • Jack said

      Try this please.

      private void initGUI() 
      {
      	setPreferredSize(new Dimension(800, 600));
      	final MigLayout migLayout = new MigLayout();
      	setLayout(migLayout);
      	final JTextArea jTextArea = new JTextArea(5, 100);
      	add(new JScrollPane(jTextArea));
      	final MessageConsole messageConsole = new MessageConsole(jTextArea);
      	messageConsole.redirectOut();
      	messageConsole.setMessageLines(100);
      	System.err.println("Hi");
      
      	{
      		final Layout layout = new TTCCLayout("yyyy-MM-dd HH:mm:ss");
      		final ConsoleAppender appender = new ConsoleAppender();
      		appender.setName(ConsoleAppender.class.getSimpleName());
      		appender.setLayout(layout);
      		appender.activateOptions();
      		Logger.getRootLogger().addAppender(appender);
      	}
      
      	final Logger logger = Logger.getRootLogger();
      	logger.info("Hello, World!");
      }
      
  12. Anonymous said

    very nice!

  13. Anonymous said

    this is great, but what exactly is textComponent?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 95 other followers

%d bloggers like this: