• category: swing folder: DecimalFormatSymbols title: JSpinnerの文字列を非表示にする tags: [JSpinner, SpinnerNumberModel, JFormattedTextField, DecimalFormatSymbols] author: aterai pubdate: 2009-08-03T20:40:16+09:00 description: SpinnerNumberModelを使用するJSpinnerを無効にしたとき、数値を非表示にします。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTKfhstbcI/AAAAAAAAAWE/MMaDVyQ9jNY/s800/DecimalFormatSymbols.png

概要

SpinnerNumberModelを使用するJSpinnerを無効にしたとき、数値を非表示にします。

サンプルコード

private static JSpinner makeSpinner1(SpinnerNumberModel m) {
  JSpinner s = new JSpinner(m);
  JFormattedTextField ftf = getJFormattedTextField(s);
  DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  ftf.setFormatterFactory(makeFFactory(dfs));
  ftf.setDisabledTextColor(UIManager.getColor("TextField.disabledColor"));
  return s;
}
View in GitHub: Java, Kotlin
private static JSpinner makeSpinner2(SpinnerNumberModel m) {
  JSpinner s = new JSpinner(m);
  JFormattedTextField ftf = getJFormattedTextField(s);
  DecimalFormatSymbols dfs = new DecimalFormatSymbols();
  dfs.setNaN(" ");
  ftf.setFormatterFactory(makeFFactory(dfs));
  return s;
}

解説

  1. デフォルト
  2. JSpinnerからJFormattedTextFieldを取得し、この無効の場合の文字色を無効場合の背景色と同じにして非表示化
  3. JSpinnerからJFormattedTextFieldを取得し、DecimalFormatSymbols#setNaNメソッドを使用して、値がNaNの場合、表示する文字列を半角スペース(U+0020)に変更
  4. JSpinnerからJFormattedTextFieldを取得し、DecimalFormatSymbols#setNaNメソッドを使用して、値がNaNの場合、表示する文字列を----に変更

参考リンク

コメント