473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,322 developers and data experts.

Text based menus

11,448 Expert 8TB
A Simple Text-Based Menu System

Read this this post; there are numerous posts like that: a newbie struggling with some sort of menu implementation. They want nested menus of course and an option to quit the entire thing. Of course they tie their 'business rules' tight together with their (feeble) attempts of their individual menu items and get heavily entangled in complicated control structures.

The post (see the link above) made me write this little article. Of course it is spoonfeeding because the code in this article can be used at will and saves all the new programmers from re-inventing the wheel again because I just did that in this article. The weather is sunny over here, I'm sitting in my back garden and it's Sunday so I have a few hours off; here goes:

The Design

I want to implement a simple text base menu system; menus can be part of another menu (or more of them). Selecting an option (a 'menu item') either opens up another menu or it activates some code. When that code has finished the same menu will be displayed again. A user must be able to navigate 'upwards' again, i.e. the menu that contains the current menu as an item will be displayed again. A user must also be able to quit the entire thing.

When you think of it: it doesn't make sense to navigate upwards when the current menu is the top menu; we'll make that navigation optional. Also we don't want the user to be able to quit everything in every single menu; we'll make that an optional feature also.

Because this is a simple text based menu system we'll display the options as simple Strings of text on the console prepended by a number like this:

Menu title:
0: first option
1: second option
2: third option
...

And we prompt the user to type 0, 1, 2, ... upto n-1 where there are n options in the menu. Of course an incorrect input deserves a warning and a retry. The 'quit' and 'back' options will be the last options (if present) in any menu.

A TextMenuItem

What exaclty is a menu item? It is represented by a single line of text and a piece of code that has to be executed when the item is selected. That's easy enough; here is the TextMenuItem class:

Expand|Select|Wrap|Line Numbers
  1. package textmenu;
  2.  
  3. public class TextMenuItem implements Runnable {
  4.  
  5.     private String title;
  6.     private Runnable exec;
  7.  
  8.     protected TextMenuItem(String title) { this(title, null); }
  9.  
  10.     public TextMenuItem(String title, Runnable exec) {
  11.         this.title= title;
  12.         this.exec= exec;
  13.     }
  14.  
  15.     public String getTitle() { return title; }
  16.  
  17.     public boolean isExec() { return exec != null; }
  18.  
  19.     protected void setExec(Runnable exec) { this.exec= exec; }
  20.  
  21.     public void run() {
  22.  
  23.         try {
  24.             exec.run();
  25.         }
  26.         catch (Throwable t) {
  27.             t.printStackTrace(System.err);
  28.         }
  29.     }
  30. }
  31.  
The constructor of this class takes a String title and an optional piece of code to be executed when the item is selected. Of course that piece of code may throw an Exception of some sort and we'll catch that and display the Exception stack trace.

I decided to model that piece of code as a Runnable. See the API documentation for all the details of that interface. You can find a link to that documentation in the first post of the 'Answers' section of the Java forum. It is strongly advised to download the documentation and keep it stored on your local harddisk somewhere, you'll need it more often than not.

Back to that class: it has a single 'getter' for the title and a 'setter' for the executable piece of code (the Runnable). We can also check whether or not such a piece of code was set previously by the 'isExec()' method.


The TextMenuItem class itself also implements the Runnable interface and (thus) it has implemented a 'run()' method. All it does is blindly call the 'run()' method from the executable piece of code and catch any Exceptions (Throwables) it may throw.

But what if the exec member variable is null? Who or what takes care of that? Think a bit: a TextMenuItem will only be selected from a menu; there are no orphan TextMenuItems. A TextMenu will take care of that. By convention only the 'back' TextMenuItem will have a null exec member variable, all other TextMenuItems must have a non-null exec member variable; that is one of the reasons why the first constructor isn't public because I don't want anyone to call it and ruin the game.

If you take a close look at that first constructor you'll see that it is a protected constructor. This implies that it can only be called from the class itself and from its sub-classes. The others (the users) have to call the second constructor.

A menu also shows itself as a single line of text in another menu (unless it is a top-level menu of course). So in a way a TextMenu is a TextMenuItem and can be a sub-class of this first class.

What's the task of a TextMenu when it is selected? Basically it has to display all of its TextMenuItems, prompt the user to select one of them and execute that TextMenuItem; all the TextMenu has to do is call its 'run()' method; with one single exception: if the TextMenuItem returns false when its 'isExec()' method is called the 'back' option was selected and the currently running TextMenu has to return from its own 'run()' method. Think a bit about this and it'll make sense.

A TextMenu

First I'll show the class (it's quite a bit bigger than the first one but it does a lot more) and then we'll dissect all the code; here it is:

Expand|Select|Wrap|Line Numbers
  1. package textmenu;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9.  
  10. public class TextMenu extends TextMenuItem {
  11.  
  12.     private static final TextMenuItem quit= new TextMenuItem("quit", new Runnable() {
  13.         public void run() {
  14.             System.exit(0);
  15.         }
  16.     });
  17.  
  18.     private static final TextMenuItem back= new TextMenuItem("back");
  19.  
  20.     List<TextMenuItem> items;
  21.  
  22.     public TextMenu(String title, TextMenuItem ... items) { this(title, false, true, items); }
  23.  
  24.     public TextMenu(String title, boolean addBack, boolean addQuit, TextMenuItem ... items) {
  25.         super(title);
  26.         setExec(this);
  27.  
  28.         initialize(addBack, addQuit, items);
  29.     }
  30.  
  31.     private void initialize(boolean addBack, boolean addQuit, TextMenuItem ... items) {
  32.  
  33.         this.items= new ArrayList<TextMenuItem>(Arrays.asList(items));
  34.         if (addBack) this.items.add(back);
  35.         if (addQuit) this.items.add(quit);
  36.     }
  37.  
  38.     private void display() {
  39.  
  40.         int option= 0;
  41.         System.out.println(getTitle()+":");
  42.         for (TextMenuItem item : items) {
  43.             System.out.println((option++)+": "+item.getTitle());
  44.         }
  45.         System.out.print("select option: ");
  46.         System.out.flush();
  47.     }
  48.  
  49.     private TextMenuItem prompt() throws IOException {
  50.  
  51.         BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  52.  
  53.         while (true) {
  54.  
  55.             display();
  56.  
  57.             String line = br.readLine();
  58.             try {
  59.                 int option= Integer.parseInt(line);
  60.                 if (option >= 0 && option < items.size())
  61.                     return items.get(option);
  62.             }
  63.             catch (NumberFormatException e) { }
  64.  
  65.             System.out.println("not a valid menu option: "+line);
  66.         } 
  67.     }
  68.  
  69.     public void run() {
  70.  
  71.         try {
  72.             for (TextMenuItem item= prompt(); item.isExec(); item= prompt())
  73.                 item.run();
  74.         }
  75.         catch (Throwable t) {
  76.             t.printStackTrace(System.out);
  77.         }
  78.     }
  79. }
  80.  
The class is stored in the same package as the first one and it extends from it so it is a TextMenuItem. More important is that it can call that first protected constructor from its super-class. It has to be able to do that because of an idiosyncracy of the Java language that forbids us something else. We'll discuss that later.

The first few lines of this class define two private static members: a 'back' menu item and a 'quit' menu item. We don't want the user to pass those in explicitly so we define them here and let the TextMenu do the work.

Both constructors take a String title and a list of TextMenuItems to populate the current menu. The second constructor also takes two boolean parameters telling it whether or not to add a 'back' menu item and/or a 'quit' menu item.

If you look at the second constructor it calls the protected constructor of its super-class, sets itself as the piece of executable code and initializes itself. Why didn't it call the other super-class constructor as in:

Expand|Select|Wrap|Line Numbers
  1. super(title, this);
  2.  
After all it represents the Runnable code associated with the menu. When a constructor calls its super-class constructor there is no 'this' yet. Java forbids those fancy things (and rightly so for reasons that are beyond this little article).

So the constructor sets just the title by calling its protected super-class constructor and sets itself as the runnable piece of code in the second step by calling the protected 'setExec()' method in its super-class. Nothing outside of the class hierarchy can do the same and we want it to be that way. That way turned Javas idiosyncracy into a good thing ...

The private 'initialize()' methods builds the entire menu: it creates a List of a copy of all the TextMenuItems and optionally adds a 'back' and a 'quit' menu item. Why does it create a copy of a list that already is a copy of all the TextMenutems? The first list is a 'write through' list (read the API documentation); this means that if someone outside these classes changes something in the array the changes will shine through in the list. We don't want that so we make a copy of that first list. The fist list is also a fixed-sized list and if we want to add other TextMenuItems to a menu later we won't be able to do that.

What happens when this menu is selected from another menu? It's 'run()' method will be called; it basically does this:

Expand|Select|Wrap|Line Numbers
  1. for (TextMenuItem item= prompt(); item.isExec(); item= prompt())
  2.     item.run();
  3.  
Here you need to know how a 'for' statement works: first the 'prompt()' method is called which returns a selected TextMenuItem; if the item is not executable the loop quits. It it is the item has its 'run()' method called and finally the user is prompted again.

In the original method body you can see that if something goes wrong (an Exception is thrown), the method catches it, displays it and terminates the execution of the current menu. What else should it do except displaying what went wrong? In a more industrial strength menu system more clever exception handling might be implemented but we'd like to keep it simple for now.

We're getting into the details of the menu handling; the body of the 'prompt()' method looks like this:

Expand|Select|Wrap|Line Numbers
  1. BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
  2.  
  3. while (true) {
  4.  
  5.     display();
  6.  
  7.     String line = br.readLine();
  8.     try {
  9.         int option= Integer.parseInt(line);
  10.         if (option >= 0 && option < items.size())
  11.             return items.get(option);
  12.     }
  13.     catch (NumberFormatException e) { }
  14.  
  15.     System.out.println("not a valid menu option: "+line);
  16.  
First it sets up a BufferedReader for the System.in stream because we want to read entire lines from the (console) input. We want the user to type an integral number between 0 and n-1 where n is the size of the 'items' list. If something goes wrong it catches the Exception, prints a warning and repeats the prompting; if all went well the selected TextMenuItem is returned. The method always displays the entire menu before user input is expected.

Note that we only catch Exceptions thrown because of incorrect input, not because some pet parrot chew the wire of your keyboard or something even more desastrous. Such events will most likely throw another type of Exception. Such Exceptions are handled by the 'run()' method that called this method and it will print the Exception stack trace and terminate as well.

The last thing that needs to be done is displaying an entire menu: the title of the TexTMenu is displayed followed by a numbered list of TextMenuItems; this is not especially rocket science and you can figure out for yourself what the following code does:

Expand|Select|Wrap|Line Numbers
  1. int option= 0;
  2. System.out.println(getTitle()+":");
  3. for (TextMenuItem item : items) {
  4.     System.out.println((option++)+": "+item.getTitle());
  5. }
  6. System.out.print("select option: ");
  7. System.out.flush();
  8.  
That's about it; all we have to do is build a little test code that plays with our menu system. Our code uses two menus, a top-level menu like this:

top menu:
0: item 1
1: nested menu
2: quit

and a nested menu like this:

nested menu:
0: item 2
1: item 3
2: back

Note that the top-level menu has a 'quit' item but no 'back' item while the nested menu has no 'quit' item but it does have a 'back' item. Here is the test code; it's a bit boring but it does what it has to do and it exercises the menu system a bit:

Expand|Select|Wrap|Line Numbers
  1. public class TestTextMenu {
  2.  
  3.     private static TextMenuItem item1= new TextMenuItem("item 1",new Runnable() {
  4.         public void run() {
  5.             System.out.println("running item 1");
  6.         }
  7.     });
  8.  
  9.     private static TextMenuItem item2= new TextMenuItem("item 2",new Runnable() {
  10.         public void run() {
  11.             System.out.println("running item 2");
  12.         }
  13.     });
  14.  
  15.     private static TextMenuItem item3= new TextMenuItem("item 3",new Runnable() {
  16.         public void run() {
  17.             System.out.println("running item 3");
  18.         }
  19.     });
  20.  
  21.     private static TextMenu nestedMenu= new TextMenu("nested menu", true, false, item2, item3);
  22.     private static TextMenu topMenu= new TextMenu("top menu", false, true, item1, nestedMenu);
  23.  
  24.     public static void main(String[] args) {
  25.  
  26.         topMenu.run();
  27.     }
  28. }
  29.  
The code builds the three TextMenuItems and the two TextMenus; the 'main()' method runs the top-level menu. That's it for now, do with the code what you want; it can be enhanced in several ways:

Display the menus and prompts by using JOptionPanes;
Have more advanced navigation options (try a 'top' item for starters);
Have help text available;
Make it behave like JMenus and JMenuItems (read the API documentation);
Make the 'quit' item a bit more polite;
Make your parrots behave more sensible.

All except the last option are quite doable; have fun.

kind regards,

Jos

ps. any discussions, please post them in the Topics>Java>Answers section; this article is closed.
May 31 '09 #1
0 11505

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Richard Patching | last post by:
Have been looking into text based UI's in Python, but have found very little useful documents. I am looking for some NCurses tutorials in particular but can't seem to find anything helpful. Can...
1
by: supportgeek | last post by:
Greetings. Would anyone know the right direction to point me on this one? I need to read text from a Windows Application based on cursor position. And enter text based on what is read. ...
5
by: Mike Mimic | last post by:
Hi! I am programming small program which would use its own text based network protocol. I come to the problem with ending lines. I was thinking about ending them with \r\n combination. But the...
1
by: Nathan Sokalski | last post by:
I have had a question that I have been avoiding or using workarounds for for a while. When using text-based controls (Labels, HyperLinks displayed as text, LinkButtons, etc.), how do I format...
5
by: Alex | last post by:
Hello, I hope I can explain this properly. I'm writing an application with a tabbed-based navigation, and a form which gets filled out by users will be split into 5 subtabs. What I need is...
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
2
by: Supremelink | last post by:
Hi all! I´m supposed to develop a text based application with .net 2005 and C#. Users shall connect to my app via Citrix, using windows CE 5.0. Any thoughts about how to do this? Should I...
3
by: Mefistofeles | last post by:
Hello. Need help to make text-based radio buttons. On my calender site I want to have text that works like radio buttons. Exampel, bold text is the selected radio-text. Jan Feb Mar Apr May...
10
oedipus
by: oedipus | last post by:
I am currently learning java at school and thus far we have oly dealt with the text-based compiler JGrasp; therefore; my attempt to code Tictactoe or "X and O's" game is written as a text based...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.