Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextField does not lose focus when SelectBox is clicked and expanded. #2173

Closed
Scott3730 opened this issue Jul 28, 2014 · 4 comments
Closed

Comments

@Scott3730
Copy link

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();
@NathanSweet
Copy link
Member

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;
    }
}

@Scott3730
Copy link
Author

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

@FredGithub
Copy link
Contributor

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

@Hafune
Copy link

Hafune commented Aug 22, 2021

Thanks!
with Kotlin

stage.root.addCaptureListener(object : InputListener() {
    override fun touchDown(event: InputEvent, x: Float, y: Float, pointer: Int, button: Int): Boolean {
        if (event.target !is TextField) stage.keyboardFocus = null
        return false
    }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants