Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4109322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:46:54+00:00 2026-05-20T21:46:54+00:00

I have a JTextPane in which text color is set as blue by default.

  • 0

I have a JTextPane in which text color is set as blue by default. Now i added strike-through on text then strike-through color becomes as same of text (blue). I want the color of text and strike-through different. e.g. if text color is blue then the strike-through must be different.

Please give me some idea.

    JTextPane text = new JTextPane();

    Font font = new Font("Serif", Font.ITALIC, 20);
    text.setFont(font); 

    text.setForeground(Color.BLUE); 

    Style style = text.addStyle("Bold", null);
    StyleConstants.setStrikeThrough(style, true);

    text.setCharacterAttributes(style, false);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:46:54+00:00Added an answer on May 20, 2026 at 9:46 pm
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.text.*;
    
    public class Test {
    
        public Test() {
            JFrame fr = new JFrame("TEST");
            fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JEditorPane pane = new JEditorPane();
            pane.setEditorKit(new NewEditorKit());
            pane.setText("test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test ");
    
            StyledDocument doc = (StyledDocument) pane.getDocument();
            MutableAttributeSet attr = new SimpleAttributeSet();
            attr.addAttribute("strike-color", Color.red);
            doc.setCharacterAttributes(0, 9, attr, false);
    
            attr.addAttribute("strike-color", Color.blue);
            doc.setCharacterAttributes(10, 19, attr, false);
            JScrollPane sp = new JScrollPane(pane);
    
            fr.getContentPane().add(sp);
            fr.setSize(300, 300);
            fr.setLocationRelativeTo(null);
            fr.setVisible(true);
        }
    
        public static void main(String[] args) {
            Test test = new Test();
        }
    }
    
    class NewEditorKit extends StyledEditorKit {
        public ViewFactory getViewFactory() {
            return new NewViewFactory();
        }
    }
    
    class NewViewFactory implements ViewFactory {
        public View create(Element elem) {
            String kind = elem.getName();
            if (kind != null) {
                if (kind.equals(AbstractDocument.ContentElementName)) {
                    return new MyLabelView(elem);
                }
                else if (kind.equals(AbstractDocument.ParagraphElementName)) {
                    return new ParagraphView(elem);
                }
                else if (kind.equals(AbstractDocument.SectionElementName)) {
                    return new BoxView(elem, View.Y_AXIS);
                }
                else if (kind.equals(StyleConstants.ComponentElementName)) {
                    return new ComponentView(elem);
                }
                else if (kind.equals(StyleConstants.IconElementName)) {
                    return new IconView(elem);
                }
            }
    
            // default to text display
            return new LabelView(elem);
        }
    }
    
    class MyLabelView extends LabelView {
    
        public MyLabelView(Element elem) {
            super(elem);
        }
    
        public void paint(Graphics g, Shape allocation) {
            super.paint(g, allocation);
            paintStrikeLine(g, allocation);
        }
    
        public void paintStrikeLine(Graphics g, Shape a) {
            Color c=(Color)getElement().getAttributes().getAttribute("strike-color");
            if (c!=null) {
                int y = a.getBounds().y + a.getBounds().height - (int) getGlyphPainter().getDescent(this);
                y = y - (int) (getGlyphPainter().getAscent(this) * 0.3f);
                int x1 = (int) a.getBounds().getX();
                int x2 = (int) (a.getBounds().getX() + a.getBounds().getWidth());
    
                Color old = g.getColor();
                g.setColor(c);
                g.drawLine(x1, y, x2, y);
                g.setColor(old);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a JTextPane to hold some text which I wish to style.
So, I have a JTextPane co_LoggerEdit = new JTextPane(); co_LoggerEdit.setContentType(text/html); I then store the
I currently have a JTextPane that will be displaying text coming in from different
I have an application which continuously receives data on a socket, and then logs
I have a JTextPane, and I'm trying to get the default font size of
I have a JTextPane and I have some text within that JTextPane. However, because
I have a JTextPane that contains a predefined text block. I have a separate
I have simple test project which creates a frame and adds one JTextPane to
I have JTextPanes which are added to a JTabbedPane. How do I get the
I have a JTextPane , and I would like to output text in it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.