Skip to content
Juuxel edited this page Jul 23, 2022 · 5 revisions

The WListPanel is used to represent a list of data in a GUI.

Getting started

To get started we need one important thing of course which is the data. The data can be any instance of a List.

    ArrayList<String> data = new ArrayList<>();
    data.add("Wolfram Alpha");
    data.add("Strange Home");

Creating the list item model

Now that we have the data, we have to insert it into the WListPanel. To do this we need to configure how each item of the list will look. We can create a model for each item in the list.

    public static class PortalDestination extends WPlainPanel {
		WSprite sprite;
		WLabel label;
		WLabel cost;
		
		public PortalDestination() {
			sprite = new WSprite(new Identifier("libgui-test:portal"));
			this.add(sprite, 2, 2, 18, 18);
			label = new WLabel(Text.literal("Foo"));
			this.add(label, 18+ 4, 2, 5*18, 18);
			cost = new WLabel(Text.literal("1000 Xp"));
			this.add(cost, 2, 20, 6*18, 18);
			
			this.setSize(7*18, 2*18);
		}
	}

We create an internal class PortalDestination that will represent an item on the list. As you can see this list item can contain multiple widgets.

Configuring the list items

We need to insert the data into the WListPanel, but we still need to configure how we want that; which piece of data goes where on our screen.

    BiConsumer<String, PortalDestination> configurator = (String s, PortalDestination destination) -> {
                destination.label.setText(Text.literal(s));
                destination.cost.setText(Text.literal("1000 xp"));
                destination.sprite.setImage(new Identifier("libgui-test:portal1.png"));
    };

Here we do exactly that. We put the values from the List into our WListPanel one by one inserting the data from our list into the PortalDestination model.

In this example, we only use a list of strings, but it could be a list of any data.

Creating the WListPanel itself

Now all that's left is to create an instance of the WListPanel and add it to the root panel.

    WListPanel<String, PortalDestination> list = new WListPanel<>(data, PortalDestination::new, configurator);
    list.setListItemHeight(2*18);
    root.add(list, 0, 2, 7, 6);