Swing/NonEditableLine のバックアップの現在との差分(No.7)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/NonEditableLine へ行く。
- 1 (2010-03-08 (月) 12:38:50)
- 2 (2010-03-08 (月) 13:43:45)
- 3 (2012-06-22 (金) 18:05:32)
- 4 (2012-12-24 (月) 00:49:28)
- 5 (2013-01-02 (水) 14:27:51)
- 6 (2013-12-24 (火) 20:19:05)
- 7 (2014-12-30 (火) 15:41:26)
- 8 (2015-01-25 (日) 18:33:41)
- 9 (2016-07-08 (金) 12:57:33)
- 10 (2017-09-26 (火) 11:02:51)
- 11 (2019-03-26 (火) 19:08:38)
- 12 (2021-01-07 (木) 16:49:10)
- 13 (2023-07-07 (金) 13:53:47)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: NonEditableLine title: JTextAreaの一部を編集不可にする tags: [JTextArea, DocumentFilter, Highlighter] author: aterai pubdate: 2010-02-22T14:50:22+09:00 description: JTextAreaの一部の行を編集不可になるよう設定します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQW4ZQhAI/AAAAAAAAAfc/JkImmzMvG6I/s800/NonEditableLine.png --- * 概要 [#qcda2065] * 概要 [#summary] `JTextArea`の一部の行を編集不可になるよう設定します。 #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTQW4ZQhAI/AAAAAAAAAfc/JkImmzMvG6I/s800/NonEditableLine.png) * サンプルコード [#d9a23670] * サンプルコード [#sourcecode] #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{ 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 { 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 { 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); } } }} * 解説 [#c7b230d4] 上記のサンプルでは、`DocumentFilter`を使って、`JTextArea`の一行目と二行目で追加、削除などの編集ができないように設定しています。 * 解説 [#explanation] 上記のサンプルでは、`DocumentFilter`を使って`JTextArea`の`1`行目と`2`行目で文字の追加、削除などの編集が不可になるよう設定しています。 #code{{ ((AbstractDocument)textArea.getDocument()).setDocumentFilter(new NonEditableLineDocumentFilter()); ((AbstractDocument) textArea.getDocument()).setDocumentFilter( new NonEditableLineDocumentFilter()); }} ---- 一行目と二行目の背景色は、編集不可のための`DocumentFilter`とは関係なく、`Highlighter`を使って別途設定しています。 - `1`行目と`2`行目の背景色は編集不可のための`DocumentFilter`とは無関係に`Highlighter`を使用して設定 #code{{ try{ try { Highlighter hilite = textArea.getHighlighter(); Document doc = textArea.getDocument(); Element root = doc.getDefaultRootElement(); for(int i=0;i<2;i++) { for (int i = 0; i < 2; i++) { Element elem = root.getElement(i); hilite.addHighlight(elem.getStartOffset(), elem.getEndOffset()-1, elem.getEndOffset() - 1, highlightPainter); } }catch(BadLocationException ble) { } catch (BadLocationException ble) { ble.printStackTrace(); } }} * 参考リンク [#j8d6a25e] * 参考リンク [#reference] - [http://www.jroller.com/santhosh/date/20050622 Document Guard - Santhosh Kumar's Weblog] - [[JTextAreaの最終行だけ編集可能になるよう設定する>Swing/LastLineEditableTextArea]] * コメント [#y8571e33] * コメント [#comment] #comment #comment