Lanterna- how do you create a simple CheckBox menu?

264 Views Asked by At

So far I have

Terminal terminal = new DefaultTerminalFactory().createTerminal();
TerminalScreen screen = new TerminalScreen(terminal);
MultiWindowTextGUI mwtg = new MultiWindowTextGUI​(screen);

CheckBoxList checkBoxList = new CheckBoxList<String>();
checkBoxList.addItem("Check one");
checkBoxList.addItem("Check two");

What I can't figure out is how to add checkBoxList directly to mwtg

Many thanks for your help

1

There are 1 best solutions below

0
George S. On

ok, so after muddling blindly through the API for hours guessing here and there, I did this, which works but is probably clunky or smells, so please feel free to improve on my own answer.

private MultiWindowTextGUI mwtg;
private BasicWindow bw;
private CheckBoxList<String> checkBoxList;
private List<String> checkedItems;
Terminal terminal = new DefaultTerminalFactory().createTerminal();
TerminalScreen screen = new TerminalScreen(terminal);
MultiWindowTextGUI mwtg = new MultiWindowTextGUI​(screen);
this.checkBoxList = new CheckBoxList<String>();
this.checkBoxList.addItem("item1");
this.checkBoxList.addItem("item2");
this.checkBoxList.addItem("item3");
this.checkBoxList.addListener((sel, prev) ->
    { this.checkedItems = this.checkBoxList.getCheckedItems​(); }
);
Panel panel = new Panel();
panel.setLayoutManager(new GridLayout(4));
panel.addComponent(this.checkBoxList);
Button button = new Button("Done", () -> this.bw.close());
button.addTo​(panel);
this.bw = new BasicWindow("Choices");
this.bw.setComponent(panel);
this.mwtg.addWindowAndWait(this.bw);

I hope this may be useful for someone...