Java – LibGDX: Leave all Actors on stage unchecked

LibGDX: Leave all Actors on stage unchecked… here is a solution to the problem.

LibGDX: Leave all Actors on stage unchecked

I have a stage with multiple buttons on it, basically used as a toolbox. I want the user to be able to choose between the different items displayed; Therefore, when the user selects an item, all other items must be deselected.

I want to do this with the checked attribute of the libGDX button. However, I don’t know how to programmatically uncheck the button and access all the Actors on the Stage in the simplest way.

I

can’t provide the code because, as I said, I don’t even know how to uncheck the button, and Google doesn’t help. Is this possible? If not, I’m happy to take other suggestions.

Solution

Take a look at ButtonGroup

ButtonGroup is not an actor and has no visuals. Buttons are added to it and it enforce a minimum and maximum number of checked buttons. This allows for buttons (button, text button, checkbox, etc) to be used as “radio” buttons. https://github.com/libgdx/libgdx/wiki/Scene2d.ui#wiki-ButtonGroup

Also try to see useful javadocs http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ButtonGroup.html

Basically, you create a ButtonGroup to add actors and set the minimum amount of checks that should be allowed.

//initalize stage and all your buttons
ButtonGroup buttonGroup = new ButtonGroup(button1, button2, button3, etc...)
next set the max and min amount to be checked
buttonGroup.setMaxCheckCount(1);
buttonGroup.setMinCheckCount(0);
it may be useful to use this method:
buttonGroup.setUncheckLast(true); If true, when the maximum number of buttons are checked and an additional button is checked, the last button to be checked is unchecked so that the maximum is not exceeded.

Related Problems and Solutions