[SOLVED] [Cp5] Do something as textfield loses focus

I would like to check the value of cp5 textfields as soon as they lose focus. The only thing I found regarding this, it that textfields have an .isFocus() method, but I cant think of a smart way to utilize this.

You would have to register, when a textfield has focus and then check if if this textfield loses its focus again. This method also has to work for multiple fields.


Edit:

After I have been thinking about this for a bit, I have become quite sure that there is no better solution than the one I’ll describe:

  • check if any Textfields have focus (Textfield.isFocus:boolean)
  • if yes, save the name or whatever of the textfield
  • keep checking if the textfield has focus
  • if it loses focus, check value

To clarify: I’m not asking for a working code of the described above, I’m just asking for a better approach.


Edit 2:
I solved this problem by simplifying it. Am going to add my code later.


Edit 3:
A few days after asking this question, i cant comprehend what I was thinking at times. I think i must have gotten confused by the examples and descriptions given in the cp5 documentation about textfields, because I was starting to implement the idea I listed above, when I noticed that I was thinking in opposites for some reason.
I was trying to check when a textfield loses focus, instead of just checking if a textfield has focus. Instead of checking the user-input after the textfield lost focus, I’m now checking while it has focus.
Basically, if it hasnt gotten clear yet:

 if (option1.isFocus()) {
    if (!option1.getText().matches("[0-9]+")) {
    // show an error
    }  else {
    // you get the point
    }    
}

(option1 being the textfield)

1 Like

Can you share your attempt please? This goes into controlP5 realm and to find an answer that address your question would require to combine the following resources:

  1. Effort from the user (you) and the forum volunteers
  2. Checking the provided examples by the library
  3. Checking controlP5’s online documentation (yes, those resources are scarce and not the best documented… but that is what we have)
  4. Working on an MCVE which is a minimum amount of code reproducing your issue. This is really useful as it removes overhead that is not needed to approach your problem. Instead of each member to create their own mcve, which might not comply with your requirements, you could take the lead and generate the first version yourself. Then other ppl in the forum, including myself, could attempt to produce a solution.
  5. If the problem is an issue with the lib, contacting the library maintainer via github ticket.
  6. Final resource, and the most important one, is to have the controlP5’s source code handy. After all, this is the concept of Open source projects…

I hope the community agrees with this approach!

Kf

2 Likes

Hello, thanks for your answer.
I understand your point but I meant to ask for an idea, not a working code example.

You would have to register, when a textfield has focus and then check if if this textfield loses its focus again.

This was the only attempt I had at that time but I have been thinking about it and updated the post accordingly.

Thanks so much, @delsey – that would be very helpful to future forum users!