JAVA-asciimenusLib is an automatic ASCII menus library designed to simplify the creation of text-based menus in Java applications. This library provides two main classes: MainMenu
and SubMenu
, which allow developers to create interactive menus that can execute methods or run specific classes.
Include the library in your Java project by copying the source files from the repository.
The MainMenu
class is designed to display a menu of options and execute the run
method of the selected option. The options are passed as an array of objects, where each object must have a public run(InputStream inputStream)
method.
Constructors:
MainMenu(Object[] options)
MainMenu(String name, Object[] options)
MainMenu(String name, String pattern, Object[] options)
Methods:
void run(InputStream inputStream)
: Executes the selected option'srun
method.
Example:
Object[] options = {new Option1(), new Option2()};
MainMenu menu = new MainMenu("Main Menu", "Option ", options);
menu.run(System.in);
The SubMenu
class creates a menu that lists methods of a specified object, allowing users to execute them directly. Custom options can be added, and certain methods can be hidden from the menu.
Constructors:
SubMenu(Object object)
SubMenu(String name, Object object)
SubMenu(String name, Object object, String[] customOptions)
SubMenu(String name, Object object, String[] customOptions, String[] hiddenOptions)
Methods:
int run(InputStream inputStream)
: Displays the menu and executes the selected method.
Example:
String[] customOptions = {"Custom Option"};
SubMenu submenu = new SubMenu("Sub Menu", this, customOptions);
int option = 0;
while(option != submenu.getExit()) {
option = submenu.run(System.in);
// Handle custom options if any
}
- Method:
void printTitle(String name)
: Prints the title of the menu.
Feel free to fork this repository and submit pull requests.
This project is open-source, licensed under the MIT License.