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

Add a clear icon click listener for ClearableTextField #1296

Open
bykka opened this issue Aug 2, 2020 · 8 comments · May be fixed by #1299
Open

Add a clear icon click listener for ClearableTextField #1296

bykka opened this issue Aug 2, 2020 · 8 comments · May be fixed by #1299

Comments

@bykka
Copy link

bykka commented Aug 2, 2020

There is no way to distinguish how the text field has been cleared - by pressing backspace or clear icon.

An idea is to add a clean icon click listener to the CustomTextField and method TextFields.createClearableTextField should return this class instead of simple TextField

@abhinayagarwal
Copy link
Member

Hi @bykka !

Is there a reason why you want to distinguish between how the TextField is cleared?

@bykka
Copy link
Author

bykka commented Aug 17, 2020

I use ClearableTextField in my query builder to the database. And of cause, I do not execute a query on every keypress.
Because a query execution is a pretty slow operation I try to execute it completely entered values.
And for my case click on clear icon equals to the cleaning everything with backspace and pressing enter at the end.
Simply clearing with backspace I consider as the user wants to change the value, so I do not need to execute a query.

@abhinayagarwal
Copy link
Member

If you want to trigger something when the TextField is empty, this piece of code should work:

textfield.lengthProperty().addListener((o, ov, nv) -> {
    if (nv == 0) {
        // Execute your query
    }
});

@bykka
Copy link
Author

bykka commented Aug 17, 2020

I do not want to trigger if backspace has been used for clearing.

@abhinayagarwal
Copy link
Member

You can update the code snippet to:

textfield.lengthProperty().addListener((o, ov, nv) -> {
    if (nv == 0 && nv - ov > 1) {
        // Execute your query
    }
});

This will only be triggered if the button is clicked or if backspace is used by selecting 2 or more characters. The snipped can be changed as per the use-case.

If you want to only execute when the button is pressed and not on key events, then add a Mouse Event handler and check the length of the TextField inside it:

textfield.addEventHandler(MOUSE_PRESSED,  e -> {
    if (textfield.getLength() == 0) {
        // Execute your query
    }
});

There are multiple ways to achieve what you are aiming to do. I am not inclined towards making an API change for this requirement.

@bykka
Copy link
Author

bykka commented Aug 17, 2020

Of cause I can find a workaround on how to make it work. It simply looks more natural to have a "clear event" for that.

textfield.addEventHandler(CLEAR_PRESSED, e -> { });

@abhinayagarwal
Copy link
Member

abhinayagarwal commented Aug 17, 2020

On a second thought, I can think of various usages for this event. Would you be interested in creating a PR?

@bykka
Copy link
Author

bykka commented Aug 17, 2020

I will try it.

bykka pushed a commit to bykka/controlsfx that referenced this issue Aug 19, 2020
bykka pushed a commit to bykka/controlsfx that referenced this issue Oct 6, 2020
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

Successfully merging a pull request may close this issue.

2 participants