I have a stage with multiple buttons on it that basically serves as a toolbox. I want the user to be able to select between the different items that are displayed; therefore when the user selects one item, all others have to be deselected.
I thought of doing that with the checked property of libGDX buttons. However, I don’t know how to programatically uncheck a button and to acces all actors on a stage in the simplest possible way.
I can’t provide code becuase as I said, I don’t even know how tp uncheck a button and google doesn’t help. Is that even possible? If not, I’d be happy about other suggestions.
Advertisement
Answer
Take a look at a 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 and take a look at the useful javadocs for it http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ButtonGroup.html
Basically you create your ButtonGroup add actors and set a minimum amount of checked things 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.