问题
In this question, the inquirer says
The JTextPane do have word wrap when text exceeded width
This does not seem to be the case.
scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
this.contentPane.add(scrollPane);
txtrFghj = new JTextPane();
txtrFghj.setBorder(null);
txtrFghj.setContentType("text/html");
txtrFghj.setText(content);
scrollPane.setViewportView(txtrFghj);
In the given code excerpt, content
's content will not wrap at all, it simply exceedes the visible size of the window. Long sentences cannot be seen completely, if the window is not big enough.
How can I achieve line wrapping?
I tried
txtrFghj.setSize(50,50);
but this does not change any behaviour at all.
回答1:
There must be something else in your code that prevents this from working correctly.
Here is a small demo example with the same code that runs just fine:
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
public class TestLineWrap {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestLineWrap().initUI();
}
});
}
protected void initUI() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane editorPane = new JTextPane();
editorPane.setContentType("text/html");
editorPane
.setText("<html>Some long text that cannot hold on a single line if the screen is too small</html>");
JScrollPane scrollPane = new JScrollPane(editorPane);
scrollPane
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
frame.add(scrollPane);
frame.setSize(200, 400);
frame.setVisible(true);
}
}
回答2:
Thanks to Guillaume, I figured it out, it took me three hours to realize though: Line break very much indeed does work here, but string break is not working, as can be seen in the thread which was quoted in the question.
My JTextPane
content looked like the following:
<html>
<body>
<br>
<font color="red">bla bla bla bla</font>\r\n
<u>someVeeeeeeeeeeeeeryLooooongString__WithOUTanySpacesInBetweeeeeeeeeeeeeeeeeeeeeeeeen</u>
<b>more text</b>
// ........ a lot of more HTML
Some funny SENTENCE which is longer than JTextPane.getSize().width usually allows. This sentence was NOT LINE WRAPPED which made me ask the question.
</body>
</html>
Now, the SENTENCE would have been line-wrapped, if the VeeeeryLooongString had not expanded the JTextPane's width. All the time, I hadn't thought of the long String within my TextPane Object and how it could have affected the overall line wrapping behaviour.
Deleting this giga-String solved the issue for me.
For further information on this topic, see this detailed question.
Now I am going to try to enable String wrapping within JTextPanes, for which further information can again be found in the quoted thread.
Edit.: Letter-wrapping can be done, but it seems line-breaks <br>
are not working anymore then. For the given question, the easiest workaround will be not to disable the horizontal scrollbar and set it to AS_NEEDED
instead:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
来源:https://stackoverflow.com/questions/27677123/jtextpane-line-break