Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I believe I understand what you're shooting for better now. You're looking to perform user input validation.</p> <p>When you're doing your user validation, there's really two ways to approach it:</p> <ol> <li>Validate immediately after any change takes place and provide feedback</li> <li>Validate when the focus leaves the input area</li> </ol> <p>With both, you'll be using property listeners - it's just a matter of what property listener you're dealing with.</p> <p>In the first case you'll listen directly to the property you're validating:</p> <pre><code> TextField field = new TextField(); field.textProperty().addListener(new ChangeListener&lt;String&gt;(){ @Override public void changed(ObservableValue&lt;? extends String&gt; value, String oldValue, String newValue) { //Do your validation or revert the value }}); </code></pre> <p>In the second case, you'll listen to the <code>focused</code> property, and validate when focus is lost (you can maintain the last validated value in this listener to help revert the value if necessary):</p> <pre><code> TextField field = new TextField(); field.focusedProperty().addListener(new ChangeListener&lt;Boolean&gt;(){ String lastValidatedValue = ""; @Override public void changed(ObservableValue&lt;? extends Boolean&gt; value, Boolean oldValue, Boolean newValue) { if(newValue == false &amp;&amp; oldValue == true){ //Do your validation and set `lastValidatedValue` if valid } }}); </code></pre> <p><strong>Note:</strong> I was assuming you just wanted to put in a fail safe for system code updating the UI. I'll leave my previous answer as I believe it provides useful information as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload