Search code examples
javalibgdx

Putting a many of buttons inside a table in scrollpane


I create a table contains a many of buttons (Actors) in scrollpane as shown in the following code:

 public class ChooseLevel implements Screen {
   private Stage stage;
   private Button[][] btnLevel;
   private Skin skin;
   private Table table;
   private ScrollPane scrollPane;
   private ScrollPaneStyle paneStyle;
@Override
public void show() {
    stage = new Stage(new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
    Gdx.input.setInputProcessor(stage);

    skin = new Skin(new TextureAtlas(Gdx.files.internal("pack.pack")));

    table = new Table();
    table.padTop(100f);

    btnLevel = new Button[50][3];

    for (int row = 0; row < 50; row++) {
        for (int col = 0; col < 3; col++) {
            btnLevel[row][col] = new Button(skin.getDrawable("up"), skin.getDrawable("down"));

            table.add(btnLevel[row][col]).pad(10f);

            if (col == 2) table.row().pad(10f);

            btnLevel[row][col].addListener(new ClickListener() {
                @Override
                public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                    return true;
                }

                @Override
                public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                    for (int row = 0; row < btnLevel.length; row++) {
                        for (int col = 0; col < btnLevel[0].length; col++) {
                            if (btnLevel[row][col].isPressed()) {
                                ((Game) Gdx.app.getApplicationListener()).setScreen(new Play());
                            }
                        }
                    }
                }
            });

        }
    }

    paneStyle = new ScrollPaneStyle(null, null, null, null, skin.getDrawable("unPressed"));
    paneStyle.vScrollKnob.setMinWidth(10f);

    scrollPane = new ScrollPane(table, paneStyle);
    scrollPane.setBounds(0, 0, stage.getWidth(), stage.getHeight());

    stage.addActor(scrollPane);

}

@Override
public void render(float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act();
    stage.draw();

}

@Override
public void resize(int width, int height) {
    stage.getViewport().update(width, height, false);

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void hide() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    stage.dispose();
    skin.dispose();

}

}

The Problem is : When I scroll up or down, the buttons listen the touching then call this line ((Game) Gdx.app.getApplicationListener()).setScreen(new Play()); before I pressed on a specific button.


Solution

  • Change this line : if (btnLevel[row][col].isPressed())

    to

    if (btnLevel[row][col].isChecked())