在文本上设置突出显示将删除该文本上的鼠标突出显示

时间:2012-01-26 11:42:07

标签: java swing

我正在突出显示文本区域中的一些文字:

Highlighter highlighter = getHighlighter();
Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(new Color(201, 197, 198));
highlighter.addHighlight(0,10, painter);

这很好用。但是,当我使用鼠标突出显示文本时,我希望使用默认的高亮颜色。当鼠标不再突出显示文本时,它将恢复为我选择的突出显示颜色new Color(201, 197, 198);

鼠标突出显示优先于我的设置突出显示?

由于

2 个答案:

答案 0 :(得分:1)

您可以定义自己的荧光笔并设置为JTextComponent。请参见DefaultHighlighter。

涂料的顺序在下面的方法中定义,但高亮显示和LayeredHighlightInfo无法覆盖(包级别)

public void paintLayeredHighlights(Graphics g, int p0, int p1,
                       Shape viewBounds,
                       JTextComponent editor, View view) {
    for (int counter = highlights.size() - 1; counter >= 0; counter--) {
        Object tag = highlights.elementAt(counter);
        if (tag instanceof LayeredHighlightInfo) {
        LayeredHighlightInfo lhi = (LayeredHighlightInfo)tag;
        int start = lhi.getStartOffset();
        int end = lhi.getEndOffset();
        if ((p0 < start && p1 > start) ||
            (p0 >= start && p0 < end)) {
            lhi.paintLayeredHighlights(g, p0, p1, viewBounds,
                           editor, view);
        }
        }
    }
}

答案 1 :(得分:1)

也许(Stas和Rob)实现了自己的荧光笔,需要根据Mouse_selection从API中过度使用Rectangle / Shape

但更舒适的是使用JTextPane和AttributeSet,但是错过那些带有彩色矩形的荧光笔

例如

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

public class ColorPane extends JTextPane {

    private static final long serialVersionUID = 1L;

    public void appendNaive(Color c, String s) { // naive implementation
        // bad: instiantiates a new AttributeSet object on each call
        SimpleAttributeSet aset = new SimpleAttributeSet();
        StyleConstants.setForeground(aset, c);
        int len = getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public void append(Color c, String s) { // better implementation--uses       
        StyleContext sc = StyleContext.getDefaultStyleContext(); // StyleContext
        AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,
                StyleConstants.Foreground, c);
        int len = getDocument().getLength(); // same value as
        //getText().length();
        setCaretPosition(len); // place caret at the end (with no selection)
        setCharacterAttributes(aset, false);
        replaceSelection(s); // there is no selection, so inserts at caret
    }

    public static void main(String argv[]) {
        UIManager.put("TextPane.selectionBackground", Color.yellow);
        UIManager.put("TextPane.selectionForeground", Color.blue);
        ColorPane pane = new ColorPane();
        for (int n = 1; n <= 400; n += 1) {
            if (isPrime(n)) {
                pane.append(Color.red, String.valueOf(n) + ' ');
            } else if (isPerfectSquare(n)) {
                pane.append(Color.blue, String.valueOf(n) + ' ');
            } else {
                pane.append(Color.black, String.valueOf(n) + ' ');
            }
        }
        JFrame f = new JFrame("ColorPane example");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setContentPane(new JScrollPane(pane));
        f.setSize(600, 400);
        f.setVisible(true);
    }

    public static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        double max = Math.sqrt(n);
        for (int j = 2; j <= max; j += 1) {
            if (n % j == 0) {
                return false; // j is a factor
            }
        }
        return true;
    }

    public static boolean isPerfectSquare(int n) {
        int j = 1;
        while (j * j < n && j * j > 0) {
            j += 1;
        }
        return (j * j == n);
    }
}

或将此代码从Caret转换为Painter

class HighlightCaret extends DefaultCaret {

    private static final Highlighter.HighlightPainter unfocusedPainter = new DefaultHighlighter.DefaultHighlightPainter(new Color(230, 230, 210));
    private static final long serialVersionUID = 1L;
    private boolean isFocused;

    @Override
    protected Highlighter.HighlightPainter getSelectionPainter() {
        return isFocused ? super.getSelectionPainter() : unfocusedPainter;
    }

    @Override
    public void setSelectionVisible(boolean hasFocus) {
        if (hasFocus != isFocused) {
            isFocused = hasFocus;
            super.setSelectionVisible(false);
            super.setSelectionVisible(true);
        }
    }
}