Creating a Custom Java Console
Here's where the string actually gets shown:
// non-static method for actually displaying new text
private void showString( Object o ) {
ta.append( o.toString() );
}
This non-static method is called on the unique object that we created above, and it simply adds the string to the end of the scrolling text window.
Voila! Console!
Hooking the System
Here's our prize feature. If our runtime environment allows it, we'd like to redirect
and
to print directly to our console.
Since this is something we don't necessarily want to in every case, we've created a method you have to call to get it going, called :
// hook stdout and stderr, and redirect output to the console
// keep the originals around, of course
static public void hookStandards() {
synchronized( hookLock ) {
if (out!=null)
return;
out = System.out;
err = System.err;
PrintStream dwout =
new PrintStream( new ConsoleOutputStream() );
System.setOut( dwout );
System.setErr( dwout );
}
}
Since we're going to want to provide the user with a way of restoring the original and (namely, Console.unhookStandards() |
), we keep them around in variables called and . And to make sure nothing goes awry in a multithreaded situation, we put a block around it.
We replace both and with a new OutputStream object called a
ConsoleOutputStream
. This is a class we've created that redirects all output back to our console. Take a look at the
source
to see how we've done it it's quite simple.
Trying It Out
We've provided two ways to try this out: an applet and an application. The applet demonstrates the use of
directly, while the application also shows the use of
and
Console.unhookStandards() |
. You can try the applet out right
here
. You can run the application by typing at the command-line.
Bonus Feature
Another nice thing you can do in the console is add your own custom debugging utilities. Some browsers provide useful things like memory dumps there's no reason we can't do that too.
If you press 'c' in the console window, you'll see that it clears the window. And if you press 'd', you'll see a nice stack dump of all running threads. Well, you might see it not all runtime environments will allow it.
Further Ideas
There are a few tidbits you might want to consider if you create a class like this one.
Typing (or , for that matter) is a lot of typing if you type it many, many times. If you don't mind a bit of obfuscation in your code, it might be good to rename to , because is shorter.
I mentioned above that this system only has one , and all output goes to this same console. This might not be ideal for your situation consider the possibility of allowing the creation of multiple consoles and allowing your code to send different messages to different consoles. It gets away from our original idea of trying to create a drop-in replacement for the system console, but that doesn't mean it's not a good idea.
Our console doesn't have a close button. It doesn't really need one, since the console is closed when the applet dies, but there's no reason not to allow the user to hide the console if they are sick of it. Remember, however, that you'll need to provide a way to get it back.
Source Code
About the Author
Greg Travis is a freelance programmer in New York City. His interest in computers can probably be traced back to the episode of "The Bionic Woman" where Jamie runs around trying to escape a building whose lights and doors are controlled by an evil artificial intelligence, which mocks her through loudspeakers. He's a devout believer in the religious idea that when a computer program works, it's a complete coincidence. He can be reached at mito@panix.com.
Page 2 of 2
This article was originally published on December 14, 2000