• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JTextAreaの一部を編集不可にする
#navi(../)
RIGHT:Posted by [[terai]] at 2010-02-22
RIGHT:Posted by &author(aterai); at 2010-02-22
*JTextAreaの一部を編集不可にする [#qcda2065]
JTextAreaの一部の行を編集不可になるよう設定します。

-&jnlp;
-&jar;
-&zip;

#screenshot
//#screenshot
#ref(http://lh4.ggpht.com/_9Z4BYR88imo/TQTQW4ZQhAI/AAAAAAAAAfc/JkImmzMvG6I/s800/NonEditableLine.png)

**サンプルコード [#d9a23670]
#code{{
#code(link){{
class NonEditableLineDocumentFilter extends DocumentFilter {
  @Override
  public void insertString(DocumentFilter.FilterBypass fb, int offset,
        String string, AttributeSet attr) throws BadLocationException {
    if(string == null) {
      return;
    }else{
      replace(fb, offset, 0, string, attr);
    }
  }
  @Override
  public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
                                               throws BadLocationException {
    replace(fb, offset, length, "", null);
  }
  @Override
  public void replace(DocumentFilter.FilterBypass fb, int offset, int length,
        String text, AttributeSet attrs) throws BadLocationException {
    Document doc = fb.getDocument();
    if(doc.getDefaultRootElement().getElementIndex(offset)<2) return;
    fb.replace(offset, length, text, attrs);
  }
}
}}

**解説 [#c7b230d4]
上記のサンプルでは、DocumentFilterを使って、JTextAreaの一行目と二行目で追加、削除などの編集ができないようになっています。

#code{{
((AbstractDocument)textArea.getDocument()).setDocumentFilter(new NonEditableLineDocumentFilter());
}}

**参考リンク [#j8d6a25e]
-[[Document Guard - Santhosh Kumar's Weblog>http://www.jroller.com/santhosh/date/20050622]]
-[http://www.jroller.com/santhosh/date/20050622 Document Guard - Santhosh Kumar's Weblog]

**コメント [#y8571e33]
- この間も俊太郎の詩をお  http://www.stlouisbusinesslist.com/business/5021837.htm?info=viagra viagra   袯袭袷   -- [[viagra]] &new{2010-03-08 (月) 12:38:50};
- この間も俊太郎の詩をお  http://www.stlouisbusinesslist.com/business/5021837.htm?info=viagra viagra   袯袭袷   -- [[viagra]] &new{2010-03-08 (月) 12:38:52};

#comment