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 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:
- New Project -> Java -> Application
- Leave Main unchecked
- Add to project new Other->Swing JavaForm
- NB: Simply adding new JForm probably won't work
- Drag Swing TextArea onto design view
- N.B.: do not use AWT TextArea - it won't work
- From '
jTextArea1
' Properties -> Events -> add 'jTextArea1keypressed
' event - Under
initComponents
add 'this.setLocationRelativeTo(null);
' to center the window. - Add header text under
initComponents
as desired - 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")) {
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;
}
...
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 unzip 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 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, 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
- 18th May, 2017: Version 1.0.0.0