如何在JEditorPane中关闭自动换行?

时间:2012-01-22 11:31:41

标签: java swing

我的JEditorPane会自动包裹文字;我不希望这样。我想要的只是一个水平条,允许用户根据需要进行编写。我怎样才能做到这一点?我尝试了几种方法。我已经覆盖了getScrollableTracksViewportWidth(),但这没有用。有没有人知道如何关闭自动换行?

4 个答案:

答案 0 :(得分:5)

快速谷歌搜索引导我this page,它通过继承文本窗格并覆盖getScrollableTracksViewportWidth()方法来实现它:

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

答案 1 :(得分:3)

答案 2 :(得分:2)

如果您可以控制进入的文本,并且您正在使用JEditorPane的功能,则可以通过html标记代码,并使用white-space:nowrap;阶梯财产。

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

答案 3 :(得分:-1)

为什么不使用jTextField?

它只有一条线。

相关问题