Swing/NonEditableLine のバックアップの現在との差分(No.2)
TITLE:JTextAreaの一部を編集不可にする
Posted by terai at 2010-02-22
JTextAreaの一部を編集不可にする
JTextAreaの一部の行を編集不可になるよう設定します。- category: swing folder: NonEditableLine title: JTextAreaの一部を編集不可にする tags: [JTextArea, DocumentFilter, Highlighter] author: aterai pubdate: 2010-02-22T14:50:22+09:00 description: JTextAreaの一部の行を編集不可になるよう設定します。 image:
概要
JTextArea
の一部の行を編集不可になるよう設定します。
- &jnlp;
- &jar;
- &zip;
Screenshot
Advertisement
#screenshot
サンプルコード
#spanend
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
class NonEditableLineDocumentFilter extends DocumentFilter {
@Override
public void insertString(DocumentFilter.FilterBypass fb, int offset,
String string, AttributeSet attr) throws BadLocationException {
if(string == null) {
return;
}else{
@Override public void insertString(
FilterBypass fb, int offset, String string, AttributeSet attr)
throws BadLocationException {
if (Objects.nonNull(string)) {
replace(fb, offset, 0, string, attr);
}
}
@Override
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException {
#spanadd
#spanend
@Override public void remove(
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 {
#spanadd
#spanend
@Override public void replace(
FilterBypass fb, int offset, int length, String text, AttributeSet attrs)
throws BadLocationException {
Document doc = fb.getDocument();
if(doc.getDefaultRootElement().getElementIndex(offset)<2) return;
if (doc.getDefaultRootElement().getElementIndex(offset) < 2) {
return;
}
fb.replace(offset, length, text, attrs);
}
}
解説
上記のサンプルでは、DocumentFilterを使って、JTextAreaの一行目と二行目で追加、削除などの編集ができないようになっています。解説
上記のサンプルでは、DocumentFilter
を使ってJTextArea
の1
行目と2
行目で文字の追加、削除などの編集が不可になるよう設定しています。
#spandel
((AbstractDocument)textArea.getDocument()).setDocumentFilter(new NonEditableLineDocumentFilter());
#spanend
#spanadd
((AbstractDocument) textArea.getDocument()).setDocumentFilter(
#spanend
new NonEditableLineDocumentFilter());
参考リンク
- Document Guard - Santhosh Kumar's Weblog
-
1
行目と2
行目の背景色は編集不可のためのDocumentFilter
とは無関係にHighlighter
を使用して設定#spanend #spanadd try { #spanend Highlighter hilite = textArea.getHighlighter(); Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); for (int i = 0; i < 2; i++) { Element elem = root.getElement(i); hilite.addHighlight(elem.getStartOffset(), elem.getEndOffset() - 1, highlightPainter); } #spanadd } catch (BadLocationException ble) { #spanend ble.printStackTrace(); #spanadd } #spanend #spanadd
コメント
- ãã®éãä¿å¤ªéã®è©©ãã http://www.stlouisbusinesslist.com/business/5021837.htm?info=viagra viagra 袯袭袷 -- viagra?
- ãã®éãä¿å¤ªéã®è©©ãã http://www.stlouisbusinesslist.com/business/5021837.htm?info=viagra viagra 袯袭袷 -- viagra?