• 追加された行はこの色です。
  • 削除された行はこの色です。
---
category: swing
folder: FirstCharToUpperCase
title: DocumentFilterで先頭文字を大文字に変換する
tags: [JTextField, DocumentFilter]
author: aterai
pubdate: 2010-03-01T12:38:41+09:00
description: DocumentFilterを使って、文字列の先頭が常に大文字になるように設定します。
image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMuU7OQ-I/AAAAAAAAAZo/jnaPTnPJY4w/s800/FirstCharToUpperCase.png
---
* 概要 [#summary]
`DocumentFilter`を使って、文字列の先頭が常に大文字になるように設定します。

#download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTMuU7OQ-I/AAAAAAAAAZo/jnaPTnPJY4w/s800/FirstCharToUpperCase.png)

* サンプルコード [#sourcecode]
#code(link){{
class FirstCharToUpperCaseDocumentFilter extends DocumentFilter {
  private final JTextComponent textField;
  protected FirstCharToUpperCaseDocumentFilter(JTextComponent textField) {
    super();
    this.textField = textField;
  }

  @Override public void remove(
      DocumentFilter.FilterBypass fb, int offset, int length)
      throws BadLocationException {
    Document doc = fb.getDocument();
    if (offset == 0 && doc.getLength() - length > 0) {
      fb.replace(length, 1, doc.getText(length, 1).toUpperCase(Locale.ENGLISH), null);
      fb.replace(length, 1,
                 doc.getText(length, 1).toUpperCase(Locale.ENGLISH), null);
      textField.setCaretPosition(offset);
    }
    fb.remove(offset, length);
  }

  @Override public void replace(
      DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
      throws BadLocationException {
        DocumentFilter.FilterBypass fb, int offset, int length,
        String text, AttributeSet attrs) throws BadLocationException {
    String str = text;
    if (offset == 0 && Objects.nonNull(text) && !text.isEmpty()) {
      str = text.substring(0, 1).toUpperCase(Locale.ENGLISH) + text.substring(1);
    }
    fb.replace(offset, length, str, attrs);
  }
}
}}

* 解説 [#explanation]
上記のサンプルでは、`JTextField`に入力された文字列の先頭一文字が、常に大文字になるように変換する`DocumentFilter`を設定しています。

----
以下のように、`JFormattedTextField` + `MaskFormatter`を使うと、%%先頭文字を削除した場合などで空白文字が挿入される(長さが決まっているから?)%% 指定した文字列長に足りない場合などでアンドゥが実行される。

- `JFormattedTextField` + `MaskFormatter`を使用すると指定した文字列長に足りない場合などでアンドゥが実行される
#code{{
JFormattedTextField field1 = new JFormattedTextField(new MaskFormatter("ULLLLLLLLLL"));
field1 = new JFormattedTextField(new MaskFormatter("ULLLLLLLLLL"));
}}

* 参考リンク [#reference]
- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/text/DocumentFilter.html DocumentFilter (Java Platform SE 8)]

* コメント [#comment]
#comment
#comment