A Simple Java Console Interface From Scratch



Developing and testing new java code using NetBeans can be daunting because the output window can rapidly become overcrowded with output and increasingly difficult to read. Most developers who use C# or C++ find that preliminary testing of new code using a Windows Form can be quite helpful and speeds development by quickly exposes the strengths and weaknesses of a given class. However, finding a simple form using purely Java was moderately difficult. This project demonstrates a simple, flexible, and easily modifiable method of creating console like window in java.


Surprisingly little can be found on searching the Internet for ‘Java Console’. I could only find one CodeProject article dealing with this problem and that one was rather complex for what should be a simple problem. NetBeans now appears to be the preferred java application development IDE. NetBeans uses STDIN, STDOUT, and STDERR streams but do not own the console instance displaying the streams. These do not include support for clearing the screen. I have utilized this simple java console-like window that overcomes those limitations, while at the same time, allows continued use of the NetBeans output window.

Using the Code

The java console window used in this article demonstrates the testing of a mathematical expression parser. But you can use any application of your choice and wire it into the console. To build the console, follow these steps in NetBeans:

  1. New Project -> Java -> Application
  2. Leave Main unchecked
  3. Add to project new Other->Swing JavaForm
  4. NB: Simply adding new JForm probably won’t work
  5. Drag Swing TextArea onto design view
  6. NB: do not use AWT TextArea – it won’t work
  7. From ‘jTextArea1‘ Properties -> Events -> add ‘jTextArea1keypressed‘ event
  8. Under initComponents add ‘this.setLocationRelativeTo(null);‘ to center the window.
  9. Add header text under initComponents as desired
  10. In jTextArea Properties -> set font, foreground and background

The simulation of a console window is accomplished by first adding the jTextArea1KeyPressed event. Be careful not to add this event to the Console itself which won’t work. Most of the heavy lifting is done within this event ;

private void jTextArea1KeyPressed(java.awt.event.KeyEvent evt) { 
if (evt.getKeyCode() == KeyEvent.VK_ENTER) { 
String[] lines = jTextArea1.getText().split("\\n"); 
int nline = lines.length; 
String sCommand = lines[nline - 1]; 
sCommand = sCommand.replaceAll("<< ", ""); 
System.out.println("sCommand =: |" + sCommand + "|"); 
if (sCommand.equals("quit")) { 
quitFlag = true; 
appendString("Are you sure (yes/no)?\n"); 

if(sCommand.equals("Yes") || sCommand.equals("yes") || sCommand.equals("y")) { 
System.out.println("User confirmed quit"); 
Path p = Paths.get("./data/varmap.dat"); 
if(sCommand.equals("No") || sCommand.equals("no") || sCommand.equals("n")) { if(quitFlag) { 
System.out.println("User cancelled quit"); 
quitFlag = false; 
jTextArea1.append("\n\n<< "); 
if (sCommand.equals("cls")) { 
int nlineCount = jTextArea1.getLineCount(); 
System.out.println("nlineCount =: " + nlineCount); 
sCommand = sCommand.replaceAll("<< ", ""); 
jTextArea1.append("<< "); 

String sMessage = "Processing '" + sCommand + "'\n"; 
... or wire in your own desired test class here

In order to run the demo, simply download and unzip the demo file, consoledemo.zip, open it, and double click on the consoledemo.jar. You must have JVM (Java Virtual Machine) installed on your machine for this to work. In addition, the data/varmap.dat file must be in the same directory as the *.jar, otherwise it won’t run.

If you want to compile and run the source code using NetBeans, download and uzip the source file, consolesrc.zip, copy it into your NetBeansProjects directory, boot NetBeans, select Open Project -> ConsoleDemo, then select Run->Build and Run (to rebuild dist/jar folder), then Run the project.

Points of Interest

I found console window extremely helpful in developing and debugging an variety of mathematical expression parsers. In this demo, I’ve used a version of a java recursive descent parser (RDP) taken from here. I’ve added a variable storage and retreival map class, JVarMap, java and a prescanner, JPreScan.java. The latter was necessary because the RDP only evaluates numeric experession. Consequently, the prescan translates any stored variables into their respective numeric values and substitutes these into the expression to be evaluated. The error processing built into the parser makes finding errors much easier, especially when used together with the NetBeans IDE Output.


Version May 18, 2017