在JEditorPane中预先换行

时间:2010-09-24 15:35:50

标签: java swing jeditorpane

我使用JEditorPane来显示样式文本,我也使用<pre>标记来保留文本中的所有空格。问题是我想让JEditorPane包装文本行,但HTMLEditorKit(或相关类)不会将文本包装在<pre>标记内。

我的意思是我想在样式表“white-space:pre-wrap”中使用行为simitar,这似乎不受HTMLEditorKit的支持。

你知道一种强制HTMLEditorKit在<pre>内包装文本的方法,可能会扩展它。

我做了一个例子来说明我想要做什么。

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);
    }
}

谢谢。

2 个答案:

答案 0 :(得分:3)

我终于做到了!

您所要做的就是阻止创建javax.swing.text.html.LineView。这是不包装的视图。

下面有一个完整的例子。

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);
    }
}

答案 1 :(得分:2)

UPDATE2(删除了现在编辑到问题中的错误答案)

您需要寻找兼容CSS2 +的HTMLEditorKit替代品。您可以永远等待JWebPane,或查看部分these answers。如果您可以灵活地冒险退出Swing,那么另一条路线就是SWT browser