1
\$\begingroup\$

How can I remove these(on the picture) actors if I only have access to root's Window methods getChildren() and getCells() ? I've tried somthing like this: removeActor(getCells().get(1).getTable().getCells().get(0).getTable()...getTable());

But it always return the root Table.

enter image description here

UPDATE

I've came to this:

Cell<FileChooser> cell = getCells().get(1);
removeActor(cell.getActor().getCells().get(0).getActor());

But now it throws nonsense Exception:

Exception in thread "LWJGL Application" java.lang.ClassCastException: com.kotcrab.vis.ui.widget.file.FileChooser$11 cannot be cast to com.kotcrab.vis.ui.widget.file.FileChooser

How class cannot be cast to its own type?

\$\endgroup\$
  • \$\begingroup\$ Just to the last sentence: the cast is not to its type, but to its anonymous inner class \$\endgroup\$ – comodoro Nov 29 '18 at 20:49
1
\$\begingroup\$

If you could keep a reference to the VerticalGroup when you add it then you could simply call remove on that Actor to remove it from its parent.

If for some reason you can't get a reference to the node when it's either created or added to the Stage Actor tree then you can traverse the actors using some sort of path to find the one you want to delete. For example; if you know that the "path" to the Actor that you want to remove is always

VisTable/VisScrollPane/VisTable/VerticalGroup    

then you can use a helper class like this one;

import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

public class StageHand {

    public static void removeByPath(Stage stage, Class...classes) {
        removeByPath(stage, new Array<Class>(classes));
    }

    public static void removeByPath(Stage stage, Array<Class> classes) {
        for(Actor actor : stage.getActors())
            removeByPath(actor, 0, classes);
    }

    private static void removeByPath(Actor actor, int index, Array<Class> classes) {
        Class<?> target = classes.get(index);
        if (actor.getClass().equals(target))
        {
            if (index == classes.size - 1)
                actor.remove();
            else {
                if (actor instanceof Group) {
                    for(Actor child : ((Group)actor).getChildren())
                        removeByPath(child, index + 1, classes);
                }
            }
        }
    }
}

And you'd call it something like this

StageHand.removeByPath(stage, VisTable.class, VisScrollPane.class, VisTable.class, VerticalGroup.class);

If you need to delete based on some other criteria (like Actor's getName method for instance), then that would be trivial to achieve as well.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I updated the question. \$\endgroup\$ – Vlad Dogadaev Feb 1 '18 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.