I’d like to fill a Listbox with clickable items, so I tried to directly put Buttons inside it, but I discovered that Listbox wants Strings…
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.BaseAppState;
import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import com.simsilica.lemur.*;
import java.util.ArrayList;
public class HelloJME3_3 extends SimpleApplication{
public static void main(String[] args) {
HelloJME3_3 app=new HelloJME3_3();
app.start(); // start the game
}
@Override
public void simpleInitApp() {
GuiGlobals.initialize(this);
GuiGlobals globals = GuiGlobals.getInstance();
stateManager.attach(new WikiAppstate());
}
class WikiAppstate extends BaseAppState {
Node stateGuiNode = new Node();
String pages[] = {
"page1",
"page2",
"page3",
"page4",
"page5",
"page6",
"page7",
"page8",
"page9",
};
ArrayList<String> unlocked = new ArrayList<>();
ListBox listBox;
@Override
protected void initialize(Application app) {
listBox = new ListBox();
//listBox.setVisibleItems(5);
listBox.setPreferredSize(new Vector3f(800, 500, 0));
listBox.setLocalTranslation((800) , 680, 0);
for (String page : pages) {
unlocked.add(page);
ActionButton b = new ActionButton(new Action() {
@Override
public void execute(Button source) {
WikiAppstate.this.wiki(page);
}
@Override
public String getName() {
return page;
}
});
listBox.getModel().add(b);
b.setPreferredSize(new Vector3f(500, 68, 0));
}
stateGuiNode.attachChild(listBox);
}
public void wiki(String page) {
System.out.println("goto " + page);
}
@Override
protected void cleanup(Application app) {
}
@Override
protected void onEnable() {
((SimpleApplication)getApplication()).getGuiNode().attachChild(stateGuiNode);
}
@Override
protected void onDisable() {
stateGuiNode.removeFromParent();
}
}
}