What I want to do is add items to an ArrayList then print. Each item needs to either be selectable, or non selectable.
The list needs to be interactable with the arrow keys.

I have the list being printed out, and thats about it. Heres my source, I have no idea what to do next.

import java.util.ArrayList;
import java.util.List;

public class Menu
{
    List<String> menuItems = new ArrayList<String>();

    public void addItem(String menuItem, boolean selectable)
    {
        //If the Item is selectable, print ' <' beside the currently selected.
        //Else, make it Unselectable (Will not display ' <' beside this, instead skip)
        menuItems.add(menuItem);
    }

    public void printMenu()
    {
        for(String menuItem : menuItems)
        {
            System.out.println(menuItem);
        }
    }

}

Each item needs to either be selectable, or non selectable

That's the next thing in your requirements, so do that next. What kind of variable would you use for selectable / not selectable? How many of those do you need? What would be a good way to store them all?

I have a boolean as a requirement to set it to be selectable if its true. But where would I store that data?

Simplest answer: how about an ArrayList of Booleans just like the array of Strings you already have?
Proper Java answer: create simple MenuItem class with its text String and selectable boolean as instance variables. Then make the ArrayList a list of instances of that class.

So this is what I have for MenuItem.
Could you explain why you would add them to the ArrayList?

package com.github.geodox.areacalculator.menu;

public class MenuItem
{
    private int id;
    private String _text;
    private boolean _selectable;
    private boolean _selected;

    public MenuItem(String text, boolean selectable)
    {
        id = id++;
        _text = text;
        _selectable = selectable;

        if(_selectable)
            System.out.println(_text + " <");
        else if(!_selectable)
            System.out.println(_text);
    }

    public int getID()
    {
        return id;
    }

    public void setSelected(boolean selected)
    {
        _selected = selected;
    }

}

Declaring like

    MenuItem rect = new MenuItem("rectangle", true);
    MenuItem squa = new MenuItem("square", true);
    MenuItem circ = new MenuItem("circle", true);
    MenuItem tria = new MenuItem("triangle", true);
    MenuItem trap = new MenuItem("trapezoid", true);
    MenuItem para = new MenuItem("parallelogram", true);

How would I make it switch out from 'rectangle <' to 'rectangle' after pressing the up or down key to goto the next, and back if selected again?

You need to add them to some kind of array or List so you can process them after creating them - eg to display them all.

You can override toString for your MenuItem class to return the text if not selected, text+"<" if selected.

You can use your setSelected method to change the selected state of each MenuItem as you arrow up and down the list (ps you may want to ignore a setSelected(true) if the MenuIem is not selectable).

Can you not just process them by calling their declared name?

Makes sense.

Would I just implements KeyListener? Then check against which key is pressed?

It's just being displayed to the console.

public MenuItem(String text, boolean selectable)
    {
        id = id++;
        _text = text;
        _selectable = selectable;

        if(_selectable)
            System.out.println(_text + " <");
        else if(!_selectable)
            System.out.println(_text);
    }

Edited 2 Years Ago by Doogledude123: Added code

This article has been dead for over six months. Start a new discussion instead.