javabuttonlibgdxclicklistener

Libgdx button clicked once


My question today is how to handle my game buttons...

I have this ClickListener on my button. What I want to achieve is when I hit the button it adds this table to the stage and when I hit the button again it removes the table from the stage. OR I would be okay with when I hit the button it adds the table then the button does nothing until I hit a different button that closes the table (like a X (close) button).

   assignButton.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y)
        {
            if(assignButton.isPressed())
            {
                stage.addActor(assignTable.getAssignTable());
            }
        }

Also I'm removing the table from the stage by doing stage.clear() and then doing stage.add(tableExample) to add back my normal table.. is there a way to remove one specific table? like stage.remove(assignTable)?


Solution

  • Why don't you try something like this:

    assignButton.addListener(new ClickListener(){
    
        boolean added = false;
    
        @Override
        public void clicked(InputEvent event, float x, float y)
        {
            if(!added) {
                stage.addActor(assignTable.getAssignTable());
                added = true;
            } else {
                stage.removeActor(assignTable.getAssignTable());
                added = false;
            }
    }