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

  • Home
  • SEARCH
  • 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 1109527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:14:57+00:00 2026-05-17T02:14:57+00:00

I am using a JEditorPane to show styled text and I am also using

  • 0

I am using a JEditorPane to show styled text and I am also using the <pre> tag to preserve all the whitespaces in the text. The problem is that I want to have the JEditorPane to wrap the text lines, but the HTMLEditorKit (or a related class) does not wrap text inside a <pre> tag.

What I mean is that I want a behavior simitar to the style sheet “white-space: pre-wrap” which seems to not be supported by HTMLEditorKit.

Do you know a way force HTMLEditorKit to wrap text inside <pre>, maybe extending it.

I made an example to show what I am trying to do.

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.text.*;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;

public class PreWrapApp extends javax.swing.JFrame {

    public class PreWrapHTMLEditorKit extends HTMLEditorKit {

        ViewFactory viewFactory = new HTMLFactory() {

            @Override
            public View create(Element elem) {
                AttributeSet attrs = elem.getAttributes();
                Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
                Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
                if (o instanceof HTML.Tag) {
                    HTML.Tag kind = (HTML.Tag) o;
                    if (kind == HTML.Tag.PRE) {
                        //View view = new javax.swing.text.html.ParagraphView(elem); // Not wrapping
                        View view = new javax.swing.text.html.ParagraphView(elem.getElement(0)); // Don't show everything
                        return view;
                    }
                }
                return super.create(elem);
            }
        };

        @Override
        public ViewFactory getViewFactory() {
            return this.viewFactory;
        }
    }

    public PreWrapApp() {
        JEditorPane editorPane = new JEditorPane();
        JScrollPane scrollPane = new JScrollPane();

        this.setPreferredSize(new Dimension(300, 300));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new BorderLayout());
        //editorPane.setContentType("text/html");
        editorPane.setEditorKit(new PreWrapHTMLEditorKit());
        editorPane.setText(""
                + "<html>"
                + "<head>"
                + "<style type=\"text/css\">"
                + "pre.c1 { white-space: normal;}" // It is not wrapping
                + "pre.c2 { white-space: pre-wrap;}" // It is not wrapping
                + "p.c3 { white-space: pre;}" // It is not preserving white-spaces
                + "</style>"
                + "</head>"
                + "<body>"
                + "<pre class=\"c1\">long text line long text line long text line long text line (new line here!) \nlong text line long text line long text line long text line</pre>"
                + "<pre class=\"c2\">long text line long text line long text line long text line (new line here!) \nlong text line long text line long text line long text line</pre>"
                + "<p   class=\"c3\">long text line long text line long text line long text line (new line here!) \nlong text line long text line long text line long text line</p>"
                + "</body>"
                + "</html>");
        scrollPane.setViewportView(editorPane);
        getContentPane().add(scrollPane);
        pack();
    }

    public static void main(String args[]) {
        new PreWrapApp().setVisible(true);
    }
}

Thank you.

  • 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-17T02:14:58+00:00Added an answer on May 17, 2026 at 2:14 am

    I finally did that!

    All you have to do is prevent the creation of the javax.swing.text.html.LineView. This is the View that does not wrap.

    There is a fully working example below.

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JEditorPane;
    import javax.swing.JScrollPane;
    import javax.swing.text.*;
    import javax.swing.text.html.HTML;
    import javax.swing.text.html.HTMLEditorKit;
    
    public class PreWrapApp extends javax.swing.JFrame {
    
        public static class PreWrapHTMLEditorKit extends HTMLEditorKit {
    
            ViewFactory viewFactory = new HTMLFactory() {
    
                @Override
                public View create(Element elem) {
                    AttributeSet attrs = elem.getAttributes();
                    Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
                    Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
                    if (o instanceof HTML.Tag) {
                        HTML.Tag kind = (HTML.Tag) o;
                        if (kind == HTML.Tag.IMPLIED) {
                            return new javax.swing.text.html.ParagraphView(elem);
                        }
                    }
                    return super.create(elem);
                }
            };
    
            @Override
            public ViewFactory getViewFactory() {
                return this.viewFactory;
            }
        }
    
        public PreWrapApp() {
            JEditorPane editorPane = new JEditorPane();
            JScrollPane scrollPane = new JScrollPane();
    
            this.setPreferredSize(new Dimension(300, 300));
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            getContentPane().setLayout(new BorderLayout());
            editorPane.setEditorKit(new PreWrapHTMLEditorKit());
            editorPane.setText(""
                    + "<html>"
                    + "<head></head>"
                    + "<body>"
                    + "<pre>long text line long text line long text line long text line (two new lines here!)\n\n"
                    + "long text line long text line long text line long text line long text line long text line</pre>"
                    + "</body>"
                    + "</html>");
            scrollPane.setViewportView(editorPane);
            getContentPane().add(scrollPane);
            pack();
        }
    
        public static void main(String args[]) {
            new PreWrapApp().setVisible(true);
        }
    }
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a JEditorPane to display some HTML data, however any images that have
I am currently using a JEditorPane to display text that is being read from
i was trying to do some simple text formatting using JEditorPane but then as
I'm using a JEditorPane to load some HTML content from a String so that
ChatGUI im using 2 JEditorPane to transfer text from one to another. once i
I am displaying text in a Java JEditorPane using HTML to fomrat the text.
I am trying to make a Text Editor using Java Swing. In that I
Using report builder 3.0, I have a report that queries a cube. How do
I want to view styled text, for example in html (not html file, just
Using CRM 4, I have an entity form that contains a tab with an

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.