Note that there are some explanatory texts on larger screens.

plurals
  1. PORestrict which values will be settleable in an javafx property?
    primarykey
    data
    text
    <p>What's the best way to keep the value of a javafx Property within specific bounds?</p> <p><i>(Or - is this bad practice, existing any reason to never filter values wrapped by javafx properties?)</i></p> <p>Example1: avoid negative values in an IntegerProperty<br> Example2: keep the value of an IntegerProperty within the bounds of a List</p> <p>First idea: - override <code>IntegerPropertyBase.set(int)</code>. It's safe? Actually <code>setValue(int)</code> only calls <code>set(int)</code>, but - if this implementation one day changes - the control over the values set goes lost.</p> <p>Second idea: - override <code>IntegerPropertyBase.invalidate()</code>. But at this point the value already was set.</p> <p>Will it fit better to javafx properties throw an <code>IllegalArgumentException</code> (or an <code>ArrayIndexOutOfBoundsException</code>, if the wrapped value is the index of an array), or better refuse the value out of bounds, setting back the last value in bounds?</p> <p>Maybe like this:</p> <pre><code> class BoundedIntegerProperty extends IntegerPropertyBase { (...) int oldValue = defaultValueInBounds; boolean settingOldValue = false; public void invalidated() { if(!settingOldValue){ if(outOfBounds(get())){ settingOldValue = true; set(oldValue); } else { oldValue = get(); } } else settingOldValue = false; } } </code></pre> <p>Only throw an Exception in invalidated() for values out of bounds may keep the value of the property out of bounds.</p> <p>Have I overlooked anything in javafx properties provided to filter values?<br></p> <p>(If necessary, please help me improving the possibly bad english of this text...)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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