user3203425
user3203425

Reputation: 3059

libgdx Table can't get ChangeListener for a click to fire?

I have a table added to a stage, and I can't get a click listener to fire for it:

Table table = new Table();
table.setX(0);
table.setY(0);
table.setWidth(300);
table.setHeight(300);
table.addListener(new ChangeListener() {
    @Override
    public void changed(ChangeEvent event, Actor actor) {
        System.out.println("click");
    }
});

stage.addActor(table);

I've set a background nine patch on the table, and it's rendering just fine. Clicking has no effect. If I add something like a label or a button to the table, then set the same ChangeListener to those widgets, the listener fires just fine.

Is there some special handling that Table needs for this to work?

Thanks

--------- Update -------------------

The only way I could get this to work was by using an EventListener to block all interaction on the Table instance:

table.addListener(new EventListener() {
    @Override
    public boolean handle(Event event) {
        return true;
    }
});

All the events would fire as expected here.

I also tried setting the table to touchable, but still the ChangeListener would not fire:

table.setTouchable(Touchable.enabled);
table.setListener(....);

So I guess I'm going to stick with the EventListener approach.

Upvotes: 4

Views: 1158

Answers (1)

Robert P
Robert P

Reputation: 9793

The Table in libgdx is not touchable by default, in its constructor setTouchable(Touchable.childrenOnly);(Take a look at the source) is called, which means, that this Actor (the `Table´) won't get any input events, but its children will (Look at the javadoc).

So to enable input events for the Table and not only for its children, you need to call setTouchable(Touchable.enabled).

I hope this solves your problem.

Upvotes: 5

Related Questions