csga5000
csga5000

Reputation: 4151

libgdx ScrollPane - Doesn't scroll?

I'm making a lobby in a multi-player game, which may have any number of players displayed in a table. I have the following code:

camera = new OrthographicCamera(WIDTH,HEIGHT);
    camera.position.set(WIDTH/2,HEIGHT/2, 0);
    camera.update();

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    stage.setCamera(camera);
    stage.setViewport(WIDTH,HEIGHT,false);

    ScrollPaneStyle paneStyle = new ScrollPaneStyle();
    paneStyle.background = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("cavebg"));
    paneStyle.vScrollKnob = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("GUI/slidernob"));
    paneStyle.hScroll = paneStyle.hScrollKnob = paneStyle.vScroll = paneStyle.vScrollKnob;

    Table container = new Table();
    table = new Table();
    ScrollPane pane = new ScrollPane(table,paneStyle);
    container.add(pane).width(WIDTH).height(HEIGHT);
    container.row();
    container.setBounds(0,0,WIDTH,HEIGHT);
    stage.addActor(container);

    font = new BitmapFont();
    color = new Color(0,0,0,1);
    style = new TextButtonStyle();
    style.font = font;
    style.up = new TextureRegionDrawable(WizardsDuel.atlas.findRegion("GUI/guibg"));
    style.fontColor = color;

    handler = new ChangeHandler();
    ArrayList<User> users = FirebaseManager.lobbyUsers;
    for(int i = 0; i < users.size() || i < 100; i++){
        TextButton tmp = new TextButton("",style);
        tmp.scale(2);
        //tmp.setText(users.get(i).name);
        tmp.setText(i+ "   asdf");
        tmp.addListener(handler);
        table.add(tmp).width(WIDTH/4f);
        if(i%2 == 1 && i > 0)
            table.row();
    }

Despite the manner in which the table clearly extends below the bottom of the screen(which should be the bottom of the scroll pane), there is no scroll bar, and clicking and dragging does nothing.

Screenshot: enter image description here

Upvotes: 5

Views: 13637

Answers (5)

The element to be scrolled must be added to the stage or group before passing it to the ScrollPane

  1. Create the necessary actor and add it to the stage or group

    val actor = Actor()
    [Stage/Group].addActor(actor)
    
  2. Create a ScrollPane and place an actor in it

    val scrollPane = ScrollPane(actor)
    
  3. Set the position and dimensions for the actor and the ScrollPane

    actor.setBounds(x,y,w,h) | scrollPane.setBounds(x,y,w,h)
    

Upvotes: 0

bemeyer
bemeyer

Reputation: 6231

You do actually need two things. An outer Container and an inner Container. You do regularly use two Tables for this. So here is a small example how I use the scrollPane. Actually with no style but it should make it clear.

this.table = new Table();
this.container = new Table();
scroll = new ScrollPane(table);
container.add(scroll).width(500f).height(500f);
container.row();

The table is the table one where you do add the stuff that should be scrollable. The container is the outer box. So you can add for example a headline above your scrollbox. The outer box (container) does need a size. Else you will not have any scrollbars or such. And you do need the inner table.

Simple example for adding:

style = new LabelStyle(font, Color.WHITE);
label = new Label(null, style);
table.add(label);
table.row();
label.setAlignment(1); // align center

give it more lines that it can show and you'll have a scroll pane. Else there is no scrollbar or such.


All in one. You just missed the outer container i think. Add it and it should work. Don't forget to sett the sizes.

Upvotes: 8

Jason Wang
Jason Wang

Reputation: 111

maybe you forget to make stage act in render(), add something like:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
    stage.draw();
}

Upvotes: 8

junfeng
junfeng

Reputation: 59

after you init the scrollpane you should call this first

    mScrollPane.layout();

Upvotes: 1

csga5000
csga5000

Reputation: 4151

My problem was that is wasn't receiving input. My I shouldn't have set the input processor in the constructor, because, the previous screen was setting the processor to null when it was "hidden", so when I moved the line to LobbyScreen's show method it happened after the previous screen was hidden, and the input is set correctly.

Upvotes: 2

Related Questions