Skip to main content

custom DefaultHightlightpainter

No replies
michel18
Offline
Joined: 2008-02-13

Hello,

I would like to write a selection text Caret for JTextComponent. But the selected text color is not paint with :

myTextComponent.getSelectedTextColor();

In my exemple, it's ORANGE. But the selected text is always paint with black or red colors.

I'm using Java 6.
I would like some help ! Thanks.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Shape;
import javax.swing.plaf.TextUI;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.Highlighter;
import javax.swing.text.JTextComponent;
import javax.swing.text.View;
import javax.swing.text.DefaultHighlighter.DefaultHighlightPainter;

@SuppressWarnings("serial")
public class ExtendedCaretSelection extends DefaultCaret {

private Highlighter.HighlightPainter caretPainter = new CaretPainter();
private Color defaultColor = Color.GRAY;

public ExtendedCaretSelection() {
super();
}

@Override
protected Highlighter.HighlightPainter getSelectionPainter() {
return caretPainter;
}

public class CaretPainter extends DefaultHighlightPainter {

public CaretPainter() {
super(defaultColor);
}

@Override
public Shape paintLayer(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c, View view) {
Rectangle alloc = bounds.getBounds();
try {
// --- determine locations ---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c, offs0);
Rectangle p1 = mapper.modelToView(c, offs1);

// --- render ---
Color color = c.getSelectionColor();
c.getSelectedTextColor();

if (color == null) {
g.setColor(defaultColor);
}
else {
g.setColor(color);
}
if (p0.y == p1.y) {
// same line, render a rectangle
Rectangle r = p0.union(p1);
g.fillRect(r.x, r.y, r.width, r.height);
return r;
}
else {
// different lines
alloc.width = c.getWidth();
int p0ToMarginWidth = alloc.x + alloc.width - p0.x;
g.fillRect(p0.x, p0.y, p0ToMarginWidth, p0.height);
if ((p0.y + p0.height) != p1.y) {
g.fillRect(alloc.x, p0.y + p0.height, alloc.width, p1.y
- (p0.y + p0.height));
}
g.fillRect(alloc.x, p1.y, (p1.x - alloc.x), p1.height);
c.repaint();
return alloc;
}
}
catch (BadLocationException e) {
// can't render
}
return null;
}

}

}

----------------------------------------------------------------------------------------------------

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

public class Go2 {

public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane editor = new JTextPane();
ExtendedCaretSelection caret = new ExtendedCaretSelection();
editor.setCaret(caret);
editor.setSelectionColor(Color.YELLOW);
editor.setSelectedTextColor(Color.ORANGE);

frame.setContentPane(editor);
frame.setVisible(true);

editor.setText("\taaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " +
"bbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbbbb.\n" +
"ccccccccccccccccccccccccc cccccccccccccccccccccccccccccccccccc.\n" +
"ddddddddddddddddddddddddddd dddddddddddddd.\n" +
"eeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeee eeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeee " +
"eeeeeeeeeeeeeeeeeeee eeeeeeeeeeeee eeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeee.");
StyledDocument doc = editor.getStyledDocument();
Style baseStyle = doc.addStyle("base", null);
StyleConstants.setFontFamily(baseStyle, "Lucida Sans Unicode");
StyleConstants.setFontSize(baseStyle, 18);
StyleConstants.setLeftIndent(baseStyle, 10f);

Style red = doc.addStyle("red", baseStyle);
StyleConstants.setForeground(red, Color.red);
StyleConstants.setBackground(red, Color.CYAN);

doc.setCharacterAttributes(22, 10, red, false);
}

}