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 9125907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:54:49+00:00 2026-06-17T06:54:49+00:00

I’m facing an annoying little bug with JTextPane and hanging indent. Here’s a simple

  • 0

I’m facing an annoying little bug with JTextPane and hanging indent.

Here’s a simple example:

public class Scrap {

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    frame.setLayout(new BorderLayout());

    JTextPane textPane = new JTextPane();

    JScrollPane scroll = new JScrollPane(textPane);

    frame.add(scroll);

    StyledDocument doc = (StyledDocument) textPane.getDocument();

    try {

        String str = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum ";

        doc.insertString(doc.getLength(), str, null);

        // Hanging indent
        MutableAttributeSet mas = new SimpleAttributeSet();
        StyleConstants.setLeftIndent(mas, 20);
        StyleConstants.setFirstLineIndent(mas, -20);
        doc.setParagraphAttributes(0, str.length(), mas, false);

    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
}
}

On my computer, with Java 7, the first row is bolder than the other rows for some reason… Anyone have ideas how to fix this?

  • 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-06-17T06:54:50+00:00Added an answer on June 17, 2026 at 6:54 am

    I got back to this, and I got it fixed! At least well enough for my needs. The problem was, as I suspected, that JTextPane drew the first line twice.

    Oracle conveniently ignored my bug report, I guess they just don’t care about Swing anymore.

    Here’s the fix (including the long word wrap fix for Java 7, which I found from somewhere):

    import javax.swing.*;
    import javax.swing.text.Element;
    import javax.swing.text.ParagraphView;
    import javax.swing.text.View;
    import javax.swing.text.ViewFactory;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.InlineView;
    import java.awt.*;
    
    /**
     * A fixed HTML Editor Kit, which fixes two things:
     * - Word wrapping of long words (bugged in Java 7)
     * - A hanging indent bug
     */
    public class FixedHtmlEditorKit extends HTMLEditorKit {
    
    @Override
    public ViewFactory getViewFactory() {
        return new HTMLEditorKit.HTMLFactory() {
            public View create(Element e) {
                View v = super.create(e);
    
                if (v instanceof InlineView) {
                    return new InlineView(e) {
                        public int getBreakWeight(int axis, float pos, float len) {
                            return GoodBreakWeight;
                        }
    
                        public View breakView(int axis, int p0, float pos, float len) {
                            if (axis == View.X_AXIS) {
                                checkPainter();
                                int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
                                if (p0 == getStartOffset() && p1 == getEndOffset()) {
                                    return this;
                                }
                                return createFragment(p0, p1);
                            }
                            return this;
                        }
                    };
                }
                else if (v instanceof ParagraphView) {
                    return new ParagraphView(e) {
                        protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) {
                            if (r == null) {
                                r = new SizeRequirements();
                            }
                            float pref = layoutPool.getPreferredSpan(axis);
                            float min = layoutPool.getMinimumSpan(axis);
                            // Don't include insets, Box.getXXXSpan will include them.
                            r.minimum = (int) min;
                            r.preferred = Math.max(r.minimum, (int) pref);
                            r.maximum = Integer.MAX_VALUE;
                            r.alignment = 0.5f;
                            return r;
                        }
    
                        private boolean allowedToPaintFirstView = true;
    
                        private float tabBase;
    
                        /*
                         * We need to override this since tabBase is private in ParagraphView.
                         */
                        @Override
                        protected float getTabBase() {
                            return tabBase;
                        }
    
                        @Override
                        protected void paintChild(Graphics g, Rectangle alloc, int index) {
                            // Don't paint the first index twice!
                            if (index == 0 && !allowedToPaintFirstView) {
                                return;
                            }
                            super.paintChild(g, alloc, index);
                        }
    
    
                        public void paint(Graphics g, Shape a) {
                            Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
    
                            tabBase = alloc.x + getLeftInset();
    
                            // line with the negative firstLineIndent value needs
                            // special handling
                            if (firstLineIndent < 0) {
                                Shape sh = getChildAllocation(0, a);
                                if ((sh != null) &&  sh.intersects(alloc)) {
                                    int x = alloc.x + getLeftInset() + firstLineIndent;
                                    int y = alloc.y + getTopInset();
    
                                    Rectangle clip = g.getClipBounds();
                                    Rectangle tempRect = new Rectangle();
                                    tempRect.x = x + getOffset(X_AXIS, 0);
                                    tempRect.y = y + getOffset(Y_AXIS, 0);
                                    tempRect.width = getSpan(X_AXIS, 0) - firstLineIndent;
                                    tempRect.height = getSpan(Y_AXIS, 0);
                                    if (tempRect.intersects(clip)) {
                                        tempRect.x = tempRect.x - firstLineIndent;
                                        allowedToPaintFirstView = true;
                                        paintChild(g, tempRect, 0);
                                        allowedToPaintFirstView = false;
                                    }
                                }
                            }
    
                            super.paint(g, a);
                        }
                    };
                }
                return v;
            }
        };
    }
    

    }

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string

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.