Swing/WholeLineHighlightPainter のバックアップ差分(No.3)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/WholeLineHighlightPainter へ行く。
- 1 (2019-09-16 (月) 01:30:30)
- 2 (2019-09-30 (月) 09:21:06)
- 3 (2019-10-29 (火) 15:05:14)
- 4 (2021-05-23 (日) 12:37:58)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: WholeLineHighlightPainter title: JEditorPaneで選択ハイライトの描画範囲を変更する tags: [JEditorPane, DefaultCaret, StyledEditorKit, HighlightPainter] author: aterai pubdate: 2019-09-16T01:29:20+09:00 description: JEditorPaneのCaretを変更して改行のみのパラグラフ上でも選択ハイライトが描画されるよう変更します。 image: https://drive.google.com/uc?id=1bwJ-w1zk4C_2cpxyF6t0Ky6K3Grn1Z8T hreflang: href: https://java-swing-tips.blogspot.com/2019/09/change-drawing-area-of-selected.html lang: en --- * 概要 [#summary] `JEditorPane`の`Caret`を変更して改行のみのパラグラフ上でも選択ハイライトが描画されるよう変更します。 #download(https://drive.google.com/uc?id=1bwJ-w1zk4C_2cpxyF6t0Ky6K3Grn1Z8T) * サンプルコード [#sourcecode] #code(link){{ class ParagraphMarkHighlightPainter extends DefaultHighlightPainter { protected ParagraphMarkHighlightPainter(Color color) { super(color); } @Override public Shape paintLayer( Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) { Shape s = super.paintLayer(g, offs0, offs1, bounds, c, view); Rectangle r = s.getBounds(); if (r.width - 1 <= 0) { g.fillRect(r.x + r.width, r.y, r.width + r.height / 2, r.height); } return s; } } class WholeLineHighlightPainter extends DefaultHighlightPainter { protected WholeLineHighlightPainter(Color color) { super(color); } @Override public Shape paintLayer( Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) { Rectangle rect = bounds.getBounds(); rect.width = c.getSize().width; return super.paintLayer(g, offs0, offs1, rect, c, view); } } }} * 解説 [#explanation] - `DefaultHighlightPainter` -- 改行マークのみのパラグラフは`1px`のみ選択ハイライト描画される - `ParagraphMarkHighlightPainter` -- 改行マークのみのパラグラフ上にも選択ハイライトを行の高さの半分描画する - `WholeLineHighlightPainter` -- 文字列選択が改行以降も継続する場合は行全体を選択ハイライト描画する * 参考リンク [#reference] - [[JTextComponentの選択ハイライトを変更>Swing/SelectionHighlightPainter]] - [[JEditorPaneで改行を表示>Swing/ParagraphMark]] - [[JTextAreaでのCaretによる選択状態表示を維持する>Swing/CaretSelectionHighlight]] * コメント [#comment] #comment #comment