Search code examples
user-interfacelibgdxscrollpane

Implementing scrollpane in Libgdx


I am trying to make a scrollable table in libgdx. Each row has 4 columns and depending on an integer total number of images in a table increases along with the rows.

I am adding images in a table, which i added to scrollpane and finally the scrollpane is added to another table which is added to the stage.

 private void setCards(int row, int columns, int wins) {

//This function adds images to the table

    int r ;
    if(wins%columns == 0)
        r=wins/columns;
    else
        r = wins/columns + 1;

    for (int j = 0; j < r; j++) {
        for (int i = 1; i <= columns; i++) {

            if (j * columns + i <= wins) {

                int k =j*columns+i;
                if(k>12)
                    k= (k%12) + 1;


                winningCard = new Texture(Gdx.files.internal("cards/winning_card" + String.valueOf(k)+ ".png"));
                Image image = new Image(winningCard);

                mTable.add(image).colspan(1).pad(10);
            }

            else {


                scoreCard = new Texture(Gdx.files.internal("cards/score_card.png"));
                Image background = new Image(scoreCard);

                mTable.add(background).colspan(1).pad(10);

            }

            mTable.pad(10);

        }
        mTable.row();
    }
}

After this i'm adding the table to scrollpane

    mTable.setFillParent(true);
    pane = new ScrollPane(mTable);
    pane.setScrollingDisabled(true,false);
    pane.setScrollBarPositions(true,false);
    pane.setFillParent(true);
    pane.setClamp(true);
    table.add(pane);
    table.setFillParent(true);
    stage.addActor(table);

The problem i'm facing is, there is a lot of blank space, i have to scroll down to get to the images. I want the screen to start at the first row.


Solution

  • Don't use setFillParent(true) on a table inside a ScrollPane. It causes your problems.

    If you want to expand your Table to the size of the scrollpane, use expand()/expandX()/expandY().