12

Trying to add multiple items to a scrollpane I quickly found that all "addActor" functions are Unsupported. So, I went with adding a table with all the items I wanted (this code misses a image that I still want to add) to make a scrollable credits screen... but this approach (currently) doesn't allow for overflow, rendering the ScrollPane useless. (My text is only shown up to what the screen's height allows, and isn't scrollable). What's the way to make a scrollable pane with multiple widgets in LibGDX? (I only care about Android and Win/Lin/Mac platforms at the moment.

    pane = new ScrollPane(null, skin);
    pane.setFillParent(true);
    paneContent = new Table(skin);
    paneContent.setFillParent(true);
    Label temp = new Label("", skin);
    temp.setAlignment(Align.left, Align.center);
    temp.setText( Gdx.files.internal("licenses/credits.txt").readString("UTF-8") );
    paneContent.addActor(temp);
    pane.setWidget(paneContent);
    stage.addActor(pane);
efaj
  • 925
  • 1
  • 12
  • 23

1 Answers1

21

If you want to put multiple items into the ScrollPane you simply need to put a table into it and call add() for each widget you want to put into the ScrollPane.

Below is an example of how to make your credits scrollable:

public class ScrollTest implements ApplicationListener {
    private Stage stage;

    private static final String reallyLongString = "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";

    @Override public void create() {
        this.stage = new Stage();
        Gdx.input.setInputProcessor(this.stage);
        final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

        final Label text = new Label(reallyLongString, skin);
        text.setAlignment(Align.center);
        text.setWrap(true);
        final Label text2 = new Label("This is a short string!", skin);
        text2.setAlignment(Align.center);
        text2.setWrap(true);
        final Label text3 = new Label(reallyLongString, skin);
        text3.setAlignment(Align.center);
        text3.setWrap(true);

        final Table scrollTable = new Table();
        scrollTable.add(text);
        scrollTable.row();
        scrollTable.add(text2);
        scrollTable.row();
        scrollTable.add(text3);

        final ScrollPane scroller = new ScrollPane(scrollTable);

        final Table table = new Table();
        table.setFillParent(true);
        table.add(scroller).fill().expand();

        this.stage.addActor(table);
    }

    @Override public void render() {
        this.stage.act();
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        this.stage.draw();
    }

    @Override public void resize(final int width, final int height) {}
    @Override public void pause() {}
    @Override public void resume() {}
    @Override public void dispose() {}
}

Edit: Added code on setting up a table inside of a ScrollPane.

Jyro117
  • 4,519
  • 24
  • 29
  • This doesn't have the desired result. You are adding the scroller into the table. The scroller still has only the text, and the other items in the table aren't scrolling. – efaj Mar 18 '13 at 21:55
  • Alright, but this is correct-ish: I was adding to the table with "addActor" instead of the proper "add" which you are using too. You just need to invert the addings: Table goes inside scroller, not the other way around. Then, I'll probably mark this as accepted. – efaj Mar 18 '13 at 22:02
  • 4
    For proper rendering and bounds checking, it's important that the table does NOT have `paneTable.setFillParent(true);` – efaj Mar 18 '13 at 22:09
  • 1
    I updated the code for multiple widgets in the scroll listener. Sorry seemed to have missed that 'key' part of your question! Silly me :( – Jyro117 Mar 18 '13 at 23:24
  • 1
    This works! Thank you very much! ..... For readers: Just don't forget to call `stage.act()` inside `void render()` – topher Aug 01 '14 at 17:38
  • do I understand something wrong, but my table (which is bigger than the screen) is not scrollable! Not with touch and also not on desktop, no scrollbars visible and also not with drag and drop. how to do that? - OK fixed: I forgot: Gdx.input.setInputProcessor(stage); in the show() method. – Suisse Nov 05 '16 at 14:40