zukes
zukes

Reputation: 411

How to turn off word wrap in JEditorPane?

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

Answers (4)

judovana
judovana

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 &nbsp;.
  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("  ", "&nbsp; ");
            line = line.replaceAll("\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
  may be usefull.*/
sb.append("</div>");
jEditorPane1.settext(sb.toString()); //jeditor pane do not support addition/insertion of text in html mode

Upvotes: 2

StanislavL
StanislavL

Reputation: 57421

Try this http://java-sl.com/wrap.html

Upvotes: 3

Jill
Jill

Reputation: 539

why don't you use a jTextField?

it's only one line.

Upvotes: -1

JB Nizet
JB Nizet

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

Related Questions