dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] JTextPane ignores <br> tag when using custom HTMLEditorKit

  1. #1
    Join Date
    Sep 2018
    Posts
    2

    [RESOLVED] JTextPane ignores <br> tag when using custom HTMLEditorKit

    Hello everyone,

    In my application I use a JTextPane embedded in a JScrollPane to display text which is formatted using HTML tags. At first I used JTextArea but then switched to JTextPane to be able to use HTML and colors. Then I needed the JTextPane to support letter based wrapping instead of just the default wrapping by white space. In other words if the content is too long it should be wrapped (just like Microsoft Word does) and the horizontal scroll bar of the JScrollPane should not be visible.

    Therefore, I found a solution in this answer that works perfectly: Letter wrap in html in JEditorPane. It uses a custom HTMLEditorKit for the JTextPane.

    But with this solution the JTextPane ignores any line breaks with the HTML tag <br>. I have to admit I don't really know how the code in the solution works. It would be great if someone could tell me how to get the HTML new line <br> tag working with this solution.

    Here is a minimal, complete & verifiable example:


    Code:
    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.*;
    
    public class JTextPaneTest extends JFrame {
    
        public static void main(String[] args) {
            new JTextPaneTest();
        }
    
        public JTextPaneTest(){
            setLayout(new BorderLayout());
    
            JTextPane textPane = new JTextPane();
            textPane.setContentType("text/html");
            textPane.setEditorKit(new HTMLEditorKitWrapSupport());  // makes JTextPane ignore <br> tag
    
            // example 1 (JTextPane ignores <br> tag when using the custom HTMLEditorKit)
            textPane.setText("<html><body style='font-size:22pt'>  <p>Line 1 <br> Line 2</p>  </body></html>");
    
            // example 2 (the text should be wrapped and the JScrollPane's horizontal bar should not be visible)
            //textPane.setText("<html><body style='font-size:25pt'>  <p>LONGWORDWITHOUTSPACES_LONGWORDWITHOUTSPACES_LONGWORDWITHOUTSPACES_LONGWORDWITHOUTSPACES_LONGWORDWITHOUTSPACES_</p>  </body></html>");
    
            JScrollPane scrollPane = new JScrollPane(textPane);
            add(scrollPane, BorderLayout.CENTER);
    
            setSize(new Dimension(500, 500));
            setVisible(true);
        }
    
        class HTMLEditorKitWrapSupport extends HTMLEditorKit {
    
            @Override
            public ViewFactory getViewFactory() {
    
                return new HTMLEditorKit.HTMLFactory() {
    
                    public View create(Element element) {
    
                        View view = super.create(element);
    
                        if (view instanceof InlineView) {
    
                            return new InlineView(element) {
    
                                @Override
                                public int getBreakWeight(int axis, float pos, float len) {
                                    return GoodBreakWeight;
                                }
    
                                @Override
                                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 (view instanceof ParagraphView) {
                            return new ParagraphView(element) {
                                protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements sizeRequirements) {
                                    if (sizeRequirements == null) {
                                        sizeRequirements = new SizeRequirements();
                                    }
    
                                    float pref = layoutPool.getPreferredSpan(axis);
                                    float min = layoutPool.getMinimumSpan(axis);
    
                                    // Don't include insets, Box.getXXXSpan will include them.
                                    sizeRequirements.minimum = (int) min;
                                    sizeRequirements.preferred = Math.max(sizeRequirements.minimum, (int) pref);
                                    sizeRequirements.maximum = Integer.MAX_VALUE;
                                    sizeRequirements.alignment = 0.5f;
                                    return sizeRequirements;
                                }
    
                            };
                        }
                        return view;
                    }
    
                };
            }
        }    
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,839

    Re: JTextPane ignores <br> tag when using custom HTMLEditorKit

    Debug the code. Put a breakpoint on @Override public View breakView () method and see if the breakpoint gets hit. If it does, it might be 'eating' the br statement. If the bp gets hit, try commentiong out this override to see if the br reappears.

  3. #3
    Join Date
    Sep 2018
    Posts
    2

    Resolved Re: JTextPane ignores <br> tag when using custom HTMLEditorKit

    Quote Originally Posted by Arjay View Post
    Debug the code. Put a breakpoint on @Override public View breakView () method and see if the breakpoint gets hit. If it does, it might be 'eating' the br statement. If the bp gets hit, try commentiong out this override to see if the br reappears.
    Thank you!

    I got help in another forum, the solution is to skip the BR tag. Here's the code:

    Code:
    @Override
            public ViewFactory getViewFactory() {
                return new HTMLEditorKit.HTMLFactory() {
                    public View create(Element element) {
                        View view = super.create(element);
                        AttributeSet attrs = element.getAttributes();
                        Object elementName = attrs.getAttribute(
                                AbstractDocument.ElementNameAttribute);
                        Object o = (elementName != null) ? null :
                                attrs.getAttribute(StyleConstants.NameAttribute);
                        HTML.Tag kind = (o instanceof HTML.Tag ? (HTML.Tag) o : null);
    
                        if (view instanceof InlineView && kind != HTML.Tag.BR) {

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)