How to set different colors for text and underline in JTextPane?

Just tried to color the text in JTextPane - but the problem cannot have different colors for the text and underline. How can I do this or is it possible? The following example prints all text and underscore in RED.

JTextPane pane = new JTextPane();

StyleContext context = new StyleContext();

Style style = pane.addStyle("Black", null);
StyleConstants.setAlignment(style, StyleConstants.ALIGN_RIGHT);
StyleConstants.setFontSize(style, 14);
StyleConstants.setSpaceAbove(style, 4);
StyleConstants.setSpaceBelow(style, 4);
StyleConstants.setForeground(style, Color.BLACK);

StyledDocument document = pane.getStyledDocument();


style = pane.addStyle("Red Underline", style);
StyleConstants.setForeground(style, Color.RED);
StyleConstants.setUnderline(style, true);

pane.getDocument().insertString(0,  "Test String", style);
+5
source share
3 answers

Here is the solution I found ...

Emphasize StyleConstant in a different color with AttributeSet

Here is a link to sample code

http://java-sl.com/tip_colored_strikethrough.html

just changing the next line solves it ...

int y = a.getBounds().y + a.getBounds().height + 2 ; 

and it works great

+4
source

Basically there are 3 classes that need to be created:

:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTextPaneUI;
import javax.swing.text.*;

public class TempProject extends JPanel{


    public static void main(String args[])    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

                //Adding pane
                JTextPane pane = new JTextPane();
                pane.setEditorKit(new CustomEditorKit());
                pane.setText("Underline With Different Color");

                //Set Style
                StyledDocument doc = (StyledDocument)pane.getDocument();
                MutableAttributeSet attrs = new SimpleAttributeSet();
                attrs.addAttribute("Underline-Color", Color.red);
                doc.setCharacterAttributes(0, doc.getLength()-1, attrs, true);

                JScrollPane sp = new JScrollPane(pane);
                frame.setContentPane(sp);  
                frame.setPreferredSize(new Dimension(400, 300));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);


            }
        });
    }

    public static class CustomEditorKit extends StyledEditorKit{

        public ViewFactory getViewFactory(){
            return new CustomUI();
        }
    }

    public static class CustomUI extends BasicTextPaneUI{
        @Override
        public View create(Element elem){
            View result = null;
            String kind = elem.getName();
            if(kind != null){
                if(kind.equals(AbstractDocument.ContentElementName)){
                    result = new MyLabelView(elem);
                } else if(kind.equals(AbstractDocument.ParagraphElementName)){
                    result = new ParagraphView(elem);
                }else if(kind.equals(AbstractDocument.SectionElementName)){
                    result = new BoxView(elem, View.Y_AXIS);
                }else if(kind.equals(StyleConstants.ComponentElementName)){
                    result = new ComponentView(elem);
                }else if(kind.equals(StyleConstants.IconElementName)){
                    result = new IconView(elem);
                } else{
                    result = new LabelView(elem);
                }
            }else{
                result = super.create(elem);
            }

            return result;
        }
    }

    public static class MyLabelView extends LabelView{

        public MyLabelView(Element arg0) {
            super(arg0);
        }

        public void paint(Graphics g, Shape a){
            super.paint(g, a);
            //Do whatever other painting here;
            Color c = (Color)getElement().getAttributes().getAttribute("Underline-Color");
            if(c != null){
                int y = a.getBounds().y + (int)getGlyphPainter().getAscent(this);
                int x1 = a.getBounds().x;
                int x2 = a.getBounds().width + x1;

                g.setColor(c);
                g.drawLine(x1, y, x2, y);
            }

        }

    }

}

:

http://java-sl.com/tip_colored_strikethrough.html

, , .

+4
  • and for html

  • Document returns the model for viewing, you can determine the index where the line started / ended

+2
source

All Articles