Libgdx: TextField does not lose focus when SelectBox is clicked and expanded.

Created on 28 Jul 2014  ·  3Comments  ·  Source: libgdx/libgdx

OS: Windows 8.1, x64
gdxVersion: 1.2.0
Targetting: Desktop only (not tested on other platforms)

When I click a SelectBox to bring up the dropdown list; a TextField in the adjacent table cell should lose focus, but it doesn't. These components share a table, and a stage. The mutual stage processes the input.

This does not really break anything, however it is undesired behaviour and can make a game look unprofessional, just the same.

Relevant code to reproduce:

    import com.badlogic.gdx.scenes.scene2d.ui.*;

    final Label titleLabel = new Label(
            "Title: ", skin, "heading");

    final Label typeLabel = new Label(
            "Type: ", skin, "heading");

    final TextField textField = new TextField("", skin);

    final SelectBox<String> contextSelector = new SelectBox<String>(skin);
    contextSelector.setItems("Weapon", "Tool", "Crop", "Machine", "World Rule");

    table.row();
        table.add(titleLabel).right();
        table.add(textField).fill().prefWidth(Gdx.graphics.getWidth() / 5.0f).space(1.0f);
    table.row();
        table.add(typeLabel).right();
        table.add(contextSelector).fill().prefWidth(Gdx.graphics.getWidth() / 5.0f).space(1.0f);
    table.row().height(15.0f); table.add().fill();
    table.row();
        table.add(placeHolderLabel1);
        table.add(startButton).right();

Most helpful comment

I'm not sure you always want the TextField to lose focus when clicking on another actor. You can customize the behavior yourself easily:

stage.getRoot().addCaptureListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        if (!(event.getTarget() instanceof TextField)) stage.setKeyboardFocus(null);
        return false;
    }
}

All 3 comments

I'm not sure you always want the TextField to lose focus when clicking on another actor. You can customize the behavior yourself easily:

stage.getRoot().addCaptureListener(new InputListener() {
    public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        if (!(event.getTarget() instanceof TextField)) stage.setKeyboardFocus(null);
        return false;
    }
}

Well, that certainly solves the issue. Cheers. :-)

Once again I found an elegant solution for my problem. scene2d is awesome :)

Was this page helpful?
0 / 5 - 0 ratings