Click here to Skip to main content
13,191,877 members (54,827 online)
Click here to Skip to main content
Add your own
alternative version

Stats

5K views
97 downloads
1 bookmarked
Posted 19 May 2017

A Simple Java Console Interface From Scratch

, 20 May 2017
Rate this:
Please Sign up or sign in to vote.
An easily modifiable console like interface for testing Java code

Introduction

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.

Background

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) { // 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"); 
               varmap.writeMap(p); 
               System.exit(0); 
          } 
     } 

     if(sCommand.equals("No") || sCommand.equals("no") || sCommand.equals("n")) { if(quitFlag) { 
          System.out.println("User cancelled quit"); 
          quitFlag = false; 
          jTextArea1.append("\n\n<< "); 
          return; 
     } 
 
     if (sCommand.equals("cls")) { 
          int nlineCount = jTextArea1.getLineCount(); 
          System.out.println("nlineCount =: " + nlineCount); 
          sCommand = sCommand.replaceAll("<< ", ""); 
          jTextArea1.setText(""); 
          jTextArea1.selectAll(); 
          jTextArea1.append("<< "); 
          return; 
     } 
     ...
// None of the above so process the command here
String sMessage = "Processing '" + sCommand + "'\n";
appendString(sMessage);
... or wire in your own desired test class here

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 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.

History

Version 1.0.0.0 May 18, 2017

License

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

Share

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.

You may also be interested in...

Comments and Discussions

 
QuestionNeeds formatting Pin
Richard MacCutchan19-May-17 21:38
protectorRichard 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
protectorRichard 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171017.2 | Last Updated 21 May 2017
Article Copyright 2017 by Michael B Pliam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid