Lemur slider how to

i load gui by url query from server (big data)
and need add slider to contaider

monstersWindow = new Container();
        monstersWindow.setLocalTranslation(widnth/2, height/2, 10);

        monstersWindow.addChild(new Label("Monsters"),0,0);
        
        String result = monstersQuery();
        
        if(!result.equals("no")){
            JSONArray jsonarray = new JSONArray(result);
            int j = 1;
            for (int i = 0; i < jsonarray.length(); i++) {
                
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                String description = jsonobject.getString("description");
                int monsterid = jsonobject.getInt("id");
                monstersWindow.addChild(new Label(description),j+1,0);

                Button slot1 = monstersWindow.addChild(new Button("Setup to slot 1"));
                Button slot2 = monstersWindow.addChild(new Button("Setup to slot 2"));
                Button slot3 = monstersWindow.addChild(new Button("Setup to slot 3"));
                Button sell = monstersWindow.addChild(new Button("Sell"));
                
                slot1.addClickCommands(new Command<Button>() {
                    @Override
                    public void execute( Button source ) {
                        
                        setupMoster(monsterid,1);
                        loadMonsters();
                    }
                });
                slot2.addClickCommands(new Command<Button>() {
                    @Override
                    public void execute( Button source ) {
                        
                        setupMoster(monsterid,2);
                        loadMonsters();
                    }
                });
                slot3.addClickCommands(new Command<Button>() {
                    @Override
                    public void execute( Button source ) {
                        
                        setupMoster(monsterid,3);
                        loadMonsters();
                    }
                }); 
                
                sell.addClickCommands(new Command<Button>() {
                    @Override
                    public void execute( Button source ) {
                        
                        sell(monsterid);
                        loadMonsters();
                    }
                }); 
                
                j = j + 5;
            }
            
        }
        Slider slider = new Slider(Axis.Y);
        
        monstersWindow.attachChild(slider);

        guiNode.attachChild(monstersWindow);

i have this result

where is the mistake?

Well, first you are using attachChild() instead of addChild() so it’s just adding it like any other spatial and its not involved in the layout at all.

…it’s not clear what you intend to do with the slider so I can’t answer further than that. The above is why it doesn’t layout with the rest of the elements.

1 Like

change attachChild() to addChild()
i have a lot of buttons and labels for example

monstersWindow = new Container();
        monstersWindow.setLocalTranslation(widnth/2, height/2, 10);
        
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        monstersWindow.addChild(new Button("Content"));
        Slider slider = new Slider(Axis.Y);
        
        monstersWindow.addChild(slider);
        
        guiNode.attachChild(monstersWindow);

i need left side slider
now buttom slider and not work

If you are trying to make a container that only shows some of your buttons with a slider to move up and down then this will not work. You should use a list box instead.

JME does not provide the facilities for Lemur to create a scroll pane. It’s just simply impossible. You can fake it with viewports and stuff but not in a way that a Lemur component could do on its own.

But for your use case, it seems like you want a list box… which is already a scrollable list of buttons.

For the future, you probably need to think about and understand how layouts work in Lemur. There are numerous examples in the Lemur demos.

1 Like

try this

        monstersWindow = new Container();
        monstersWindow.setLocalTranslation(widnth/2, height/2, 10);
        ListBox listBox = new ListBox();
        
        ActionButton b = new ActionButton(new Action() {
                    @Override
                    public void execute(Button source) {
                        
                    }

                    @Override
                    public String getName() {
                        return "test 1";
                    }

                });
        listBox.getModel().add(b);
        listBox.getModel().add(b);

listBox.setVisibleItems(3);
listBox.setPreferredSize(new Vector3f(200, 200, 0));
            listBox.setLocalTranslation((800) , 680, 0);
            
        monstersWindow.addChild(listBox);

The list box is already made of buttons. You don’t have to add buttons.

I don’t have time to step-by-step take you through every line of code today. You are going to have to look at the demos and examples and try to figure things out a little on your own.

1 Like

link to examples please
I did not find

Step 1: GitHub - jMonkeyEngine-Contributions/Lemur: Lemur is a jMonkeyEngine-based UI toolkit.

Step 2: Lemur/examples at master · jMonkeyEngine-Contributions/Lemur · GitHub

image

Step 3: Lemur/examples/demos at master · jMonkeyEngine-Contributions/Lemur · GitHub

1 Like

find this

listBox = window.addChild(new ListBox());
        listBox.setVisibleItems(5);
 
        for( int i = 0; i < 10; i++ ) {        
            listBox.getModel().add("Item " + nextItem);
            nextItem++;
        }        

make this

        monstersWindow = new Container();
        monstersWindow.setLocalTranslation(widnth/2, height/2, 10);
        ListBox listBox = new ListBox();
        

        for( int i = 0; i < 10; i++ ) {        
            listBox.getModel().add("Item " + i);
        }   
listBox.setVisibleItems(6);
listBox.setPreferredSize(new Vector3f(200, 200, 0));
            listBox.setLocalTranslation((800) , 680, 0);
            
        monstersWindow.addChild(listBox);

but how to add click action on button?

Maybe:
ListBox (lemur-proto 1.12.0 API)…-

1 Like

find this example
listBox.addClickCommands(source → System.out.println("######## "));

but how to add different click action for each button

I create class

public class ListBoxItem {
    public String text;
    
    public ListBoxItem(String text){
        this.text = text;
    }
    
    public String toString(){
        return text;
    }
    
    public void action(){
        
    }
    
}

and add to list box

        for( int i = 0; i < 10; i++ ) {        
            listBox.getModel().add(new ListBoxItem("Item " + i));
        }   
listBox.addClickCommands(new Command() {
            @Override
            public void execute(Object source) {
                System.out.println("######## " +source;
            }
        });

how i can get clicked item and execute action method?

The clicked item will be the current selection.

1 Like

i understand but how to get selected item in this place

listBox.addClickCommands(new Command() {
            @Override
            public void execute(Object source) {
                System.out.println("get selected item ");
            }
        });

Use the proper generic type information and it will be obvious.

You will have to fill in the gaps in your java knowledge as you go.

1 Like

i did it thanks

public class ListBoxItem {
    public String text;
    
    public ListBoxItem(String text){
        this.text = text;
    }
    
    public String toString(){
        return text;
    }
    
    public void action(){
       System.out.println("Click " + text);
    }
    
}
monstersWindow = new Container();
        monstersWindow.setLocalTranslation(widnth/2, height/2, 10);
        ListBox listBox = new ListBox();
        

        for( int i = 0; i < 10; i++ ) {        
            listBox.getModel().add(new ListBoxItem("Item " + i));
        }   
        listBox.addClickCommands(new Command() {
            @Override
            public void execute(Object source) {
                ListBox list = (ListBox) source;
                ListBoxItem boxItem = (ListBoxItem) list.getSelectedItem();
                boxItem.action();
            }
        });        

listBox.setVisibleItems(6);
listBox.setPreferredSize(new Vector3f(200, 200, 0));
            listBox.setLocalTranslation((800) , 680, 0);
            
        monstersWindow.addChild(listBox);

In Java there are these things called “generic types”. If you use them properly then in some cases you can avoid casting. The command is one such case:

        listBox.addClickCommands(new Command<ListBox>() {
            @Override
            public void execute(ListBox lis) {
                ListBoxItem boxItem = (ListBoxItem) list.getSelectedItem();
                boxItem.action();
            }
        });        
3 Likes