Click here to Skip to main content
15,305,039 members
Articles / Programming Languages / Java
Posted 19 May 2017


1 bookmarked

A Simple Java Console Interface From Scratch

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
20 May 2017CPOL3 min read
An easily modifiable console like interface for testing Java code

Image 1


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 exposing 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 does 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. N.B.: 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) { // Enter was pressed. Your code goes here. 

     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"); 
          // finalizing code goes here return; 

     if(sCommand.equals("Yes") || sCommand.equals("yes") || sCommand.equals("y")) { 
          if(quitFlag) { 
               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("<< "); 
// None of the above so process the command here
String sMessage = "Processing '" + sCommand + "'\n";
... or wire in your own desired test class here

If you want to compile and run the source code using NetBeans, download and unzip the source file,, 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 a 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 retrieval map class, JVarMap, java and a prescanner, The latter was necessary because the RDP only evaluates numeric expressions. 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.


  • 18th May, 2017: Version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael B Pliam
Software Developer PliaTech
United States United States
Software developer with interests in mathematics and statistics, particularly as applied to health care. CTO and founder of PliaTech.

Comments and Discussions

QuestionNeeds formatting Pin
Richard MacCutchan19-May-17 21:38
mveRichard MacCutchan19-May-17 21:38 
AnswerRe: Needs formatting Pin
Michael B Pliam20-May-17 13:03
MemberMichael B Pliam20-May-17 13:03 
AnswerRe: Needs formatting Pin
Michael B Pliam21-May-17 6:19
MemberMichael B Pliam21-May-17 6:19 
GeneralRe: Needs formatting Pin
Richard MacCutchan21-May-17 6:30
mveRichard MacCutchan21-May-17 6:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.