Reputation: 411
My JEditorPane
automatically wraps words; I don't want that. All I want is a horizontal bar to appear that allows the user to write as much as desired. How can I do that? I have tried several methods. I have overridden the getScrollableTracksViewportWidth()
, but that didn't help. Does any one know how I can turn off the word wrap?
Upvotes: 2
Views: 4866
Reputation: 417
If you can control the text which is going into, and you are using features of JEditorPane, you can mark the code via html, and use white-space:nowrap; stile property.
jEditorPane1.setContentType("text/html");
StringBuilder sb = new StringBuilder();
sb.append("<div style='");
if (!wordWrap.isSelected()) { //some checkbox
sb.append("white-space:nowrap;");
}
sb.append("font-family:\"Monospaced\">'");
sb.append("your very interesting long and full of spaces text");
/*be aware, more then one space in row will be replaced by single space
to avoid it you need to substitute by .
Also rememberer that \n have to be repalced by <br>
so filering like:
line = line.replaceAll("\n", "<br>\n"); //be aware, <br/> do not work
line = line.replaceAll(" ", " ");
line = line.replaceAll("\t", " ");
may be usefull.*/
sb.append("</div>");
jEditorPane1.settext(sb.toString()); //jeditor pane do not support addition/insertion of text in html mode
Upvotes: 2
Reputation: 691993
A quick google search lead me to this page, which implements it by subclassing the text pane and overriding the getScrollableTracksViewportWidth()
method:
// Override getScrollableTracksViewportWidth
// to preserve the full width of the text
public boolean getScrollableTracksViewportWidth() {
Component parent = getParent();
ComponentUI ui = getUI();
return parent != null ? (ui.getPreferredSize(this).width <= parent
.getSize().width) : true;
}
Upvotes: 5