Swing/BigDecimalSpinnerModel のバックアップ差分(No.1)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/BigDecimalSpinnerModel へ行く。
- 1 (2014-01-20 (月) 02:06:20)
- 2 (2014-01-20 (月) 14:03:15)
- 3 (2014-01-22 (水) 20:40:01)
- 4 (2014-09-17 (水) 02:21:19)
- 5 (2014-10-21 (火) 01:46:10)
- 6 (2015-11-12 (木) 22:04:59)
- 7 (2017-04-07 (金) 13:51:51)
- 8 (2017-04-26 (水) 13:55:28)
- 9 (2017-06-27 (火) 13:51:02)
- 10 (2018-03-22 (木) 19:26:10)
- 11 (2019-04-15 (月) 15:52:46)
- 12 (2020-05-14 (木) 16:17:07)
- 13 (2021-11-03 (水) 02:27:08)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:JSpinnerの上下限値をBigDecimalで比較する #navi(../) #tags(JSpinner, BigDecimal, SpinnerNumberModel) RIGHT:Posted by &author(aterai); at 2014-01-20 * JSpinnerの上下限値をBigDecimalで比較する [#n4090d81] `JSpinner`で浮動小数点型のモデルを使用する場合、最大値と最小値の比較を`BigDecimal`で行うよう変更します。 #download #ref(https://lh6.googleusercontent.com/-JztoRl3kot0/UtvN48iHZ3I/AAAAAAAAB-g/D3QcAYgr_ks/s800/BigDecimalSpinnerModel.png) ** サンプルコード [#daf48439] #code(link){{ class BigDecimalSpinnerModel extends SpinnerNumberModel { public BigDecimalSpinnerModel( double value, double minimum, double maximum, double stepSize) { super(value, minimum, maximum, stepSize); } @Override public Object getPreviousValue() { return incrValue(-1); } @Override public Object getNextValue() { return incrValue(+1); } private Number incrValue(int dir) { Number v = getNumber(); BigDecimal value = new BigDecimal(v.toString()); BigDecimal stepSize = new BigDecimal(getStepSize().toString()); BigDecimal maximum = new BigDecimal(getMaximum().toString()); BigDecimal minimum = new BigDecimal(getMinimum().toString()); BigDecimal newValue; if(dir>0) { newValue = value.add(stepSize); }else{ newValue = value.subtract(stepSize); } if(maximum != null && maximum.compareTo(newValue) < 0) { return null; } if(minimum != null && minimum.compareTo(newValue) > 0) { return null; } return newValue; } } }} ** 解説 [#qc1f5b7e] - 上: `SpinnerNumberModel` -- `Double`型の`SpinnerNumberModel`では、最大最小値の比較に、`Double#compare(...)`が使用されている -- 浮動小数点の誤差のせいで、下限(29.6)にダウンボタンを使用して移動できない --- `29.7 - 29.6 - 0.1 >= 0`は偽 - 下: `BigDecimalSpinnerModel` -- `SpinnerNumberModel#getPreviousValue()`などをオーバーライドして、`Double#compareTo(Double)`ではなく、`BigDecimal#compareTo(BigDecimal)`で最小値との比較を行う ** 参考リンク [#q409bb3d] - [http://stackoverflow.com/questions/21158043/jspinner-not-showing-minimum-value-on-pressing-down-arrow java - JSpinner not showing minimum value on pressing down arrow - Stack Overflow] - [[SpinnerNumberModelに上限値を超える値を入力>Swing/SpinnerNumberModel]] ** コメント [#h3f60a35] #comment