Java developers can spice up their applications by adding a Java Console. This article demonstrates how to add a Java Console to their applications.
By default, Java applications direct output and error streams to the startup window. Therefore, all calls to methods in System.out (such as System.out.println) and System.err (stacktrace in case of unhandled exceptions) objects result in data being printed in the start up window while the application is running.
For GUI-driven Java applications, it is always convenient to direct such data to another GUI component which can be activated on-demand. Java Console is a Java custom component that comes handy for such purposes.
(If you've ever seen the Java Console in Netscape Communicator 4.0x, you know what I mean by Java Console.)
Follow this link for the source of the sample Java application that has the Java Console attached to it. Here is the screenshot of the Java Console.
![]()
This application performs simple integer division of two given operands and prints the result on the standard output device by calling System.out.println method. The two edit fields "Operand1" and "Operand2" collect the operands and the "Divide" button does the division. However, exceptions can occur due to invalid data (e.g., non-numeric or very large numbers - NumberFormatException) or illegal data for the division operation (e.g., zero second operand - ArithmeticException). Here is a screenshot of the GUI of the demo application.
![]()
Download the source and compile Errors.java.
Run this application (by invoking java Errors and enter any data in the two edit fields and press "Divide." Press the "Java Console" button to bring up the Java Console. You'll see the results of your calculations in there. Try with some illegal data (say, a zero for the second operand), and see the stacktrace in the Console.
The other buttons let you enable or disable the Console or dump the contents.
- Dump Output: This button dumps the contents of the Console for the System.out stream onto the default output device (usually the startup window).
- Dump Errors: This button dumps the contents of the Console for the System.err stream onto the default error divide (usually the startup window).
- Enable Output: This checkbox enables/disable the System.out stream of the Java Console.
- Enable Error: This checkbox enables/disable the System.err stream of the Java Console.
To spice up your Java applications with the Java Console, all that is required is to create an instance of the Console component, and a mechanism to bring it up on demand.
Note that this does not work for Java applets due to security violations. Browsers will normally not let redirection of error and output streams.
Errors.java: The demo application. Console.java: The Java Console. ObservableStream.java StreamObserver.java
Note that Console.java, ObservableStream.java and StreamObserver.java should be copied into a directory (COM/Subrahmanyam/utils or COM\Subrahmanyam\utils) under your working directory. Run javadoc on Console.java if you require HTML documentation of the Java Console.
Last modified: Fri Jun 18 11:50:42 India Standard Time 1999
© Subbu Allamaraju 1998, 1999. All rights reserved.
All copyrights and trademarks acknowledged.