For the latest stable version, please use Spring Shell 3.3.3!spring-doc.cn

ListView

ListView is a base implementation providing functionality to draw a list of items. Inherits BoxView.spring-doc.cn

ListView<T> is typed as its item and can take any object. Further item processing happens in a CellFactory. For conveniance there is a support for generic higher level list feature showing checked states as normal check and radio types. Essentially what you can have is a list of items which are shown as is, shown where any items can have a checked state or only one item can have a checked state.spring-doc.cn

ListView<String> view = new ListView<>();
view.setItems(List.of("item1", "item2"));

Default item style is nocheck but can be changed.spring-doc.cn

Supports NOCHECK, `CHECK and `RADIO`spring-doc.cn

ListView<String> view = new ListView<>(ItemStyle.CHECKED);

Customisation

How individual cells are shown depends on a CellFactory. Default implementation simply shows item using its toString() method.spring-doc.cn

It can be customised by modifying a used CellFactory.spring-doc.cn

record ExampleData(String name) {
};

static class ExampleListCell extends AbstractListCell<ExampleData> {

	public ExampleListCell(ExampleData item) {
		super(item);
	}

	@Override
	public void draw(Screen screen) {
		Rectangle rect = getRect();
		Writer writer = screen.writerBuilder().style(getStyle()).build();
		writer.text(getItem().name(), rect.x(), rect.y());
		writer.background(rect, getBackgroundColor());
	}
}

And set it as a factory:spring-doc.cn

ListView<ExampleData> view = new ListView<>();
view.setCellFactory((list, item) -> new ExampleListCell(item));

Default Bindings

Default view commands are:spring-doc.cn

Table 1. ViewCommands
Command Description

LINE_UPspring-doc.cn

Selection moves up.spring-doc.cn

LINE_DOWNspring-doc.cn

Selection moves down.spring-doc.cn

Default key bindigs are:spring-doc.cn

Table 2. Key
Command Description

CursorUpspring-doc.cn

Bound ViewCommand LINE_UPspring-doc.cn

CursorDownspring-doc.cn

Bound ViewCommand LINE_DOWNspring-doc.cn

Enterspring-doc.cn

Choose active item.spring-doc.cn

Spacespring-doc.cn

Change active item selection state.spring-doc.cn

Default mouse bindigs are:spring-doc.cn

Table 3. Mouse
Command Description

Wheel | WheelUpspring-doc.cn

Bound ViewCommand LINE_UPspring-doc.cn

Wheel | WheelDownspring-doc.cn

Bound ViewCommand LINE_DOWNspring-doc.cn

Released | Button1spring-doc.cn

Choose itemspring-doc.cn

Events

Events are sent depending on a used list type.spring-doc.cn

Table 4. ListView Events
Event Description

ListViewOpenSelectedItemEventspring-doc.cn

Active item is requested to get opened.spring-doc.cn

ListViewSelectedItemChangedEventspring-doc.cn

Active item is changed.spring-doc.cn