Libgdx table can't get ChangeListener for click to fire?

I have a table added to the scene and I cannot get a click listener 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 have installed background ninth patch on the table and this rendering is just fine. Clicking has no effect. If I add something like a shortcut or a button to the table, then set the same ChangeListener on these widgets, the listener will just stop.

Is there any special handling that is required for the table to work?

thank

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

The only way I could get this to work was to use an EventListener to block all interaction on the table instance:

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

      

All events will fire as expected.

I also tried to tweak the table by touch, but still the ChangeListener didn't work:

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

      

So I guess I'll stick with the EventListener approach.

+3


source to share


1 answer


Table

in libgdx by default not touchable

, in its constructor setTouchable(Touchable.childrenOnly);

(Look at source ) which means this Actor

("Table") will not receive any input events, but its children will (Look at the javadoc ).

So, to enable input events for Table

, not just its children, you need to call setTouchable(Touchable.enabled)

.



Hope this solves your problem.

+4


source







All Articles