0
votes

I'm kind of new to the LibGDX API, and I was trying to organize labels and ImageButtons inside tables (and tables inside those tables). When I run the app, the ImageButtons are not being drawn onto the screen.

I've searched everywhere and couldn't find an answer, so I'd really appreciate help.

EDIT: It might happen because I am using tables inside tables, but I didn't find anything about nested tables, or anything like that to be honest.

I call a method in render to keep the labels' text changing that looks like this:

public void drawTable(){
    final Table scrollTable = new Table();
    scrollTable.addActor(canbtn);
    scrollTable.row();
    //other buttons

    Label.LabelStyle style = new Label.LabelStyle(font, Color.WHITE);
    Label.LabelStyle style2 = new Label.LabelStyle(font2, Color.WHITE);

    CustomLabel amtLabel = new CustomLabel("" + (int)(game.tot.getSum()), style);
    CustomLabel mizcoins = new CustomLabel("Mizcoins", style2);
    CustomLabel mpsCount = new CustomLabel(game.totMPS + " MPS", style2);

    PerSec can = game.boosts.get("Can").getPerSec();

    Table t = new Table();
    t.add(drawer(can, canbtn));
    t.row();
    scroller = new ScrollPane(t);

    table = new Table(); //the container table
    table.setFillParent(true);
    table.add(amtLabel).padTop(50);
    table.row();
    table.add(mizcoins);
    table.row();
    table.add(mpsCount);
    table.row();
    table.add(scroller).fill().expand();//.space(350).padBottom(10);
}

And the drawer() function:

public Table drawer(PerSec perSec, ImageButton imgbtn){
    Label.LabelStyle style = new Label.LabelStyle(font2, Color.WHITE);
    Label.LabelStyle style2 = new Label.LabelStyle(font2, Color.WHITE);
    Label.LabelStyle style3 = new Label.LabelStyle(font2, Color.RED);

    CustomLabel nameLabel = new CustomLabel(perSec.getType(), style);
    CustomLabel priceLabel = new CustomLabel("Price: " + perSec.getPrice(), style2);
    CustomLabel addsLabel = new CustomLabel("Adds: " + perSec.getDefAddPerSec(), style2);
    CustomLabel youHaveLabel = new CustomLabel("You Have: " + perSec.getAmt(), style3);

    Table mainTable = new Table();
    Table smallTable = new Table();

    smallTable.add(priceLabel);
    smallTable.row();
    smallTable.add(youHaveLabel);

    mainTable.add(nameLabel);
    mainTable.add(imgbtn);
    mainTable.row();
    mainTable.add(smallTable);
    mainTable.add(addsLabel);

    return mainTable;

}
1
A few things that pop out are: you first do "addActor" on the canbtn and then in "drawer()" you add it to something else. This overrules the previous add call because an actor can only have 1 parent. Also add and addActor do not work the same way. addActor places it as a child only. whereas add() creates a table cell where it is added to. making it use the table's alignment functions. The problem could be: overriding the add, adding it to the wrong table. Or adding it with a size of 0. - p.streef
I couldn't quite understand what you were going for, because I think I'm stuck on what I think I did. I'll paste a link to show you what I was going for with the design: imgur.com/a/tpVnZ - Bar Eliezer

1 Answers

0
votes

Some pseudocode to show you how I would go about creating such a table (as described in the image in your comment)

private void setupScrollPane()
{
    ScrollPane pane = new ScrollPane(generatePanels());
    pane.setBounds(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
    stage.addActor(pane);
}

private Table generatePanels()
{
    Table list = new Table();
    for(int i=0;i<2;i++)
    {
        list.add(genetePanel()).row();
    }
    return list;
}

private Table genetePanel()
{
    Table root = new Table();
    root.setSize(400,200);

    Table left = new Table();
    Table middle = new Table();
    Table right = new Table();

    left.add().growY().row();       //an empty row for padding
    left.add(new Label("Price label")).row();
    left.add(new Label("you have label")).row();
    left.add().growY().row();       //an empty row for padding

    middle.add(new Label("Name label")).row();
    middle.add().growY().row(); //an empty row for padding

    right.add(new ImageButton()).row();
    right.add(new Label("Adds label")).row();

    root.add(left).growX();
    root.add(middle).growX();
    root.add(right).growX();
}