在jTextPane中更改选定文本的颜色 [英] Changing color of selected text in jTextPane

查看:188
本文介绍了在jTextPane中更改选定文本的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JTextPane创建一个文本编辑器,允许用户更改所选文本的颜色。但是当用户选择文本时,选择选项以更改颜色(例如,更改为红色),文本不会显示为红色,直到文本未选中。我尝试使用setSelectedTextColor来更改所选文本的颜色,但是这不工作,因为这会将文本更改为红色,随时选择文本。有选择的文本显示方式,因为它是实际的颜色吗?或者像在Word中工作的方式,它不是文本的实际颜色,但是当选择不同颜色的文本时,即使在选择时它们也显示为不同的颜色。



我使用以下代码设置JTextPane和将选定文本更改为红色的按钮:

  JButton redButton = new JButton(new StyledEditorKit.ForegroundAction(red,Color.RED)); 
redButton.setFocusable(false);
buttonPanel.add(redButton);

JTextPane与内容类型HTML一样设置,并使用HTMLEditorKit:

  p = new JTextPane(); 
p.setSize(300,300);
kit = new HTMLEditorKit();
p.setEditorKit(kit);
p.setDocument(kit.createDefaultDocument());

p.setContentType(text / html);
p.setEditable(true);

让我知道如果你需要更多的源代码来理解这个问题。谢谢!

解决方案

查看 DefaultHighlightPainter DefaultHighlighter



方法

  public void paint(Graphics g,int offs0,int offs1,Shape bounds,JTextComponent c){
Rectangle alloc = bounds.getBounds();
try {
// ---确定位置---
TextUI mapper = c.getUI();
Rectangle p0 = mapper.modelToView(c,offs0);
Rectangle p1 = mapper.modelToView(c,offs1);

// --- render ---
color color = getColor();

if(color == null){
g.setColor(c.getSelectionColor());
}
else {
g.setColor(color);
}

正如你可以看到,它使用 getColor getSelectionColor()



或者使用更简单的方法来覆盖 JTextPane getSelectionColor()。在该方法中,只检查文本是否被选中,并使用所选元素的属性来获得所需的ccolor。如果没有选择只返回 super.getSelectedColor()



UPDATE:
用于低级GlyphView的
public void paint(Graphics g,Shape a){
...
JTextComponent tc =(JTextComponent)c;
颜色selFG = tc.getSelectedTextColor();

  if(//有一个荧光笔(错误4532590) 
(tc.getHighlighter()!= null)&&
//所选文字颜色不同于正常前景
(selFG!= null)&&!selFG.equals (fg)){

Highlighter.Highlight [] h = tc.getHighlighter()。getHighlights();
if(h.length!= 0){
boolean initialized = false;
int viewSelectionCount = 0;
for(int i = 0; i Highlighter.Highlight highlight = h [i];
int hStart = highlight.getStartOffset();
int hEnd = highlight.getEndOffset();
if(hStart> p1 || hEnd< p0){
//选择不在此视图中
continue;
}
if(!SwingUtilities2.useSelectedTextColor(highlight,tc)){
continue;
}

...



在源 http://kickjava.com/src/com/sun/java/swing/SwingUtilities2。 java.htm

  public static boolean useSelectedTextColor(Highlighter.Highlight JavaDoc h,JTextComponent JavaDoc c){
Highlighter.HighlightPainter JavaDoc painter = h.getPainter();
String JavaDoc painterClass = painter.getClass()。getName();
if(painterClass.indexOf(javax.swing.text.DefaultHighlighter)!= 0&&
painterClass.indexOf(com.sun.java.swing.plaf.windows.WindowsTextUI )!= 0){
return false;
}
try {
DefaultHighlighter.DefaultHighlightPainter JavaDoc defPainter =
(DefaultHighlighter.DefaultHighlightPainter JavaDoc)painter;
if(defPainter.getColor()!= null&
!defPainter.getColor()。equals(c.getSelectionColor())){
return false;
}
} catch(ClassCastException JavaDoc e){
return false;
}
return true;
}

所以使用颜色取决于L& F和画家。如果你定义你的onw画家,颜色将不会被使用。


I am creating a text editor using a JTextPane that allows the user to change the color of selected text. But when the user selects the text, then chooses the option to change the color (say, to red) the text does not appear as red until the text is unselected. I tried using setSelectedTextColor to change the color of the selected text, but that doesn't work since that changes the text to red anytime text is selected afterwards. Is there a way to have selected text show up as it's actual color? Or like the way it works in Word where it's not the actual color of the text, but when text of different colors are selected they show up as different colors even when selected.

I use the following code to set up the JTextPane and button that changes the selected text to red:

JButton redButton = new JButton(new StyledEditorKit.ForegroundAction("red", Color.RED));
redButton.setFocusable(false);
buttonPanel.add(redButton);

The JTextPane is set up as with content type HTML and uses the HTMLEditorKit:

p=new JTextPane();
p.setSize(300, 300);
kit = new HTMLEditorKit();
p.setEditorKit(kit);
p.setDocument(kit.createDefaultDocument());

p.setContentType("text/html");
p.setEditable(true);

Let me know if you need more source code to understand the question. Thank You!

解决方案

Take a look at the DefaultHighlightPainter inner class of DefaultHighlighter.

The method

    public void paint(Graphics g, int offs0, int offs1, Shape bounds, JTextComponent c) {
        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 = getColor();

            if (color == null) {
                g.setColor(c.getSelectionColor());
            }
            else {
                g.setColor(color);
            }

As you can see it uses either getColor() or getSelectionColor(). You can extend the class and adapt the highlight painting.

Or use a simpler approach to override your JTextPane's getSelectionColor(). In the method just check whether text is selected and use attributes of selected elements to get desired ccolor. If nothing is selected just return super.getSelectedColor()

UPDATE: Actually applying colors for selection is used on low level GlyphView's public void paint(Graphics g, Shape a) { ... JTextComponent tc = (JTextComponent) c; Color selFG = tc.getSelectedTextColor();

        if (// there's a highlighter (bug 4532590), and
            (tc.getHighlighter() != null) &&
            // selected text color is different from regular foreground
            (selFG != null) && !selFG.equals(fg)) {

            Highlighter.Highlight[] h = tc.getHighlighter().getHighlights();
            if(h.length != 0) {
                boolean initialized = false;
                int viewSelectionCount = 0;
                for (int i = 0; i < h.length; i++) {
                    Highlighter.Highlight highlight = h[i];
                    int hStart = highlight.getStartOffset();
                    int hEnd = highlight.getEndOffset();
                    if (hStart > p1 || hEnd < p0) {
                        // the selection is out of this view
                        continue;
                    }
                    if (!SwingUtilities2.useSelectedTextColor(highlight, tc)) {
                        continue;
                    }

...

As you can see applying selection color vs default color of the view is defined in the SwingUtilities2.useSelectedTextColor(highlight, tc)

In the sources http://kickjava.com/src/com/sun/java/swing/SwingUtilities2.java.htm

 public static boolean useSelectedTextColor(Highlighter.Highlight  JavaDoc h, JTextComponent  JavaDoc c) {
     Highlighter.HighlightPainter  JavaDoc painter = h.getPainter();
     String  JavaDoc painterClass = painter.getClass().getName();
     if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
             painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
         return false;
     }
     try {
         DefaultHighlighter.DefaultHighlightPainter  JavaDoc defPainter =
                 (DefaultHighlighter.DefaultHighlightPainter  JavaDoc) painter;
         if (defPainter.getColor() != null &&
                 !defPainter.getColor().equals(c.getSelectionColor())) {
             return false;
         }
     } catch (ClassCastException  JavaDoc e) {
         return false;
     }
     return true;
 }

So using the color depends on L&F and painter. If you define your onw painter the color won't be used.

这篇关于在jTextPane中更改选定文本的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆