Add buttons to scrollpane dynamically

5 messages - 1437 views

I want to populate a scroll pane dynamically with a number of buttons. This is not working for me as I guess I am doing something wrong. I have a code snippet here

 

    public static void load() {
        final Screen screen = MyScreens.getScreen("/xml/testing.xml");
        screen.setTitle("Buttons");
        ScrollPane item = new ScrollPane();
        for(int i = 0; i < 40; i++) {
            Button b = new Button("Foo" + i);
            item.add(b);
        }
        screen.add(item);
        screen.setCurrent();
    }

 

and testing.xml is defined thus

<screen style="layout:gridlayout(1, 6);" title="Intact: Menu">
    <screenfirstmenu onAction="logout">Logout</screenfirstmenu>
</screen>

 

This shows the screen with the title buttons, but no button will show.

 

UPDATE:

<screen style="layout:gridlayout(1, 40);" title="Intact: Menu">
    <screenfirstmenu onAction="logout">Logout</screenfirstmenu>
    <!--<screenSecondMenu onAction="next">Next</screenSecondMenu>-->
</screen>

1. remove the style="layout:gridlayout(1, 40);" because buttons are added to the scrollpane, not the screen itself.

2. new Button("XXX") means that XXX is the tag of the widget. And this is not what you want, because you want a button label !

Tag is the link between <button> and a Button() instance.

Then try :

for(int i = 0; i < 40; i++) {
   Button b = new Button();
   b.add(new Text().setText("Foo" + i));
   item.add(b);
}

Thanks for that. Another question is if I want to add custom stylying to the buttons also at runtime? how do I go about doing that. I tried this and ot doesnt seem to have any effect

b.setAttribute("bg-color", "red");

 

bg-color is not an attribute, but a style property. Attributes can be found in the "Attribute" table of each widget doc.

Then in your case the attribue for styling is style :

b.setAttribute("style", "bg-color:red");

But styling is a special case, because you can do :

b.parseAuthorStyle("bg-color:red");