To my game I want to add a gui element that displays text and textButtons whenever something is happening or it wants you to make a decission (an answer while talking to a person for example). To keep track of your actions you shall be able to scroll it. Similar to text adverntures or dialog boxes in rpgs.

I use Scene2Dui for my GUI, so I put a scrollPane in my GUIs root table. I have methods for adding stuff to the contained table and tell the scrollPane to scroll to the bottom.

In general the idea works, but the thing doesn't scroll to the bottom but only to the bottom of the element I added before the current one. I'm puzzled.

Here's the code, leaving out the skins and styles part:

public class TextBox {

ScrollPane box;
Table table;
Label.LabelStyle lStyle;
Skin skin;

TextBox() {


    box = new ScrollPane(table, scrollStyle);

    box.setScrollingDisabled(true, false);
    box.setOverscroll(false, false);


void writeDecissions(GameEvent[] events) {

    Table tableD = new Table();

    if (events != null) {

        for (int i = 1; i < events.length; i++) {

            if (events[i] != null) {

                String s = Character.forDigit(i, 10) + " - "
                        + events[i].getDecision();

                TextButton button = new TextButton(s, style);

                tableD.add(button).width(box.getWidth() - 40).spaceBottom(20).spaceTop(20);





This is the way I test it:

public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 0);


    if (Gdx.input.justTouched()) {

I tried box.setScrollY(box.getMaxY) as well as setting overscroll to true (don't really know what that is tough). With the same result.

Finally a screenshot: enter image description here

Help appreciated.


I found an answer on Stack Overflow.

The thing is I had to call layout() on the scrollPane manually.

The reason: Because the height of a wrapped label is unknown until layout() is automaticaly called while drawing. The maximum Y scroll position remains the same until this happens.


layout() should be called twice, for sure...

From label.setWrap documentation: In some cases the parent of the label will need to layout twice: once to set the width of the label and a second time to adjust to the label's new preferred height.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.