Coloration de la ligne contenant le curseur dans un jtextpane

Description

Comme le dit le titre, il s'agit d'un exemple très simple de coloration de la ligne contenant le curseur. On peut trouver ce type de coloration dans de nombreux éditeur comme le célébre borland JBuilder. Le code n'est pas commenté puisque très court et simple à comprendre

Source / Exemple :


import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import javax.swing.event.*;

public class LineColor extends JFrame implements CaretListener
{

    private static final Color HIGHLIGHT_COLOR = Color.green;

    private static int  HIGHLIGHT_OFFSET = 1;

    public LineColor() {
        super("LineColor");

        JTextPane tp = new JTextPane();
        tp.setEditorKit(new LineColorEditorKit());

        String tpText;
        tpText = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5\nLine 6\nLine7\n";
        tpText += "Line 8\nLine 9\nLine 10\nLine 11\nLine 12\n";
        tpText += "Line 13\nLine 14\nLine 15\nLine 16\nLine 17\n";
        tpText += "Line 18\nLine 19\nLine 20\nLine 21\nLine 22\n";
        tpText += "Line 23\nLine 24\nLine 25\nLine 26\nLine 27\n";

        tp.setText(tpText);
        tp.addCaretListener(this);
        tp.setCaretPosition(HIGHLIGHT_OFFSET-1);
        getContentPane().add(new JScrollPane(tp));
    }

    public void caretUpdate(CaretEvent e)
    {
      HIGHLIGHT_OFFSET=e.getDot()+1;
      repaint();
    }

    public static void main(String[] args) {
        LineColor lc = new LineColor();
        lc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        lc.setSize(400, 400);
        lc.setVisible(true);
    }

    static class LineColorEditorKit extends StyledEditorKit
                                            implements ViewFactory {

        public ViewFactory getViewFactory() {
            return this;
        }

        public View create(Element elem) {
            String kind = elem.getName();

            if (AbstractDocument.ParagraphElementName.equals(kind)) {
                return new LineColorParagraphView(elem);
            }

            return super.getViewFactory().create(elem);
        }
    }

    static class LineColorParagraphView extends ParagraphView {
        private Rectangle tempRect = new Rectangle();

        LineColorParagraphView(Element elem) {
            super(elem);
        }

        public void paint(Graphics g, Shape allocation) {
            Rectangle alloc = (allocation instanceof Rectangle) ?
                               (Rectangle)allocation :
                               allocation.getBounds();
            int n = getViewCount();
            int x = alloc.x + getLeftInset();
            int y = alloc.y + getTopInset();
            Rectangle clip = g.getClipBounds();
            for (int i = 0; i < n; i++) {
                tempRect.x = x + getOffset(X_AXIS, i);
                tempRect.y = y + getOffset(Y_AXIS, i);
                tempRect.width = getSpan(X_AXIS, i);
                tempRect.height = getSpan(Y_AXIS, i);
                if (tempRect.intersects(clip)) {
                    View v = getView(i);

                    // this is where the bg highlight is painted
                    if (v.getStartOffset() < HIGHLIGHT_OFFSET &&
                               HIGHLIGHT_OFFSET <= v.getEndOffset()) {

                        g.setColor(HIGHLIGHT_COLOR);
                        g.fillRect(tempRect.x, tempRect.y,
                                   alloc.width, tempRect.height);
                    }

                    paintChild(g, tempRect, i);
                }
            }
        }
    }
}

Conclusion :


La couleur de coloration de la ligne peut être facilement changée, on se lasse vite du vert...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.