-->

jtextpane doesn't wrap text

2019-02-16 02:37发布

问题:

I've got a problem with JTextPane. I need to mark some parts of text with specified color, so I've decided to use JTextPane and html tags to decorate my text. JTextPane is inside JScrollPane, and JScrollPane is inside JSplitPane:

JTextPane jtp=new JTextPane();
jtp.setContentType("text/html");
JScrollPane scr=new JScrollPane(jtp);
JSplitPane leftRight=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, scr, someOtherComponent);

Also, after setting text into jtp I've noticed that it wraps uncorrectly.

So, could you advice me how to solve my problem, or, maybe offer a better solution?

P.S. I decorate text using html :

<FONT style="BACKGROUND-COLOR: yellow">next marked</FONT><b> embolden</b> normal<FONT style="BACKGROUND-COLOR: yellow"> next marked</FONT>

maybe there are any other swing components that helps do such simple text decoration?

P.P.S. Here is part of my code:

    originalTextArea=new JTextPane();        
    originalTextArea.setFont(font);
    originalTextArea.setContentType("text/html");
    originalTextArea.setText("dhjfsfdjnkjfgfjkgkjfngfdkjnjfdgjfdngfdkjgnkdngjgnjkgfgf");

    processedTextArea=new JTextPane();        
    processedTextArea.setFont(font);
    processedTextArea.setContentType("text/html");              

    JScrollPane originalTextScrollPane=new JScrollPane(originalTextArea);

    JScrollPane processedTextScrollPane=new JScrollPane(processedTextArea);
    JTabbedPane processedTextAndVocPane=new JTabbedPane(JTabbedPane.TOP, JTabbedPane.SCROLL_TAB_LAYOUT);
    summarizedTextAndVocPane.add("Processed text",processedTextScrollPane);

    JSplitPane leftRightSplitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, originalTextScrollPane, processedTextAndVocPane);
    leftRightSplitPane.setDividerLocation(0.5);
    leftRightSplitPane.setResizeWeight(0.5);        

    mainFrame.add(leftRightSplitPane);

After input text into originalTextArea it doesnt wrap at all

回答1:

http://java-sl.com/tip_html_letter_wrap.html That shows how to add letter wrap support



标签: java swing wrap