2012-01-22 30 views
1

我的JEditorPane自動換行;我不想那樣。我只想要一個水平條出現,允許用戶儘可能多地寫入。我怎樣才能做到這一點?我嘗試了幾種方法。我已經覆蓋getScrollableTracksViewportWidth(),但這沒有幫助。有沒有人知道我可以關掉這個詞包裝?如何關閉JEditorPane中的自動換行?

+1

如果您不需要'JEditorPane'中的功能,那麼可以使用支持'setLineWrap()'方法的'JTextArea'。 –

+1

交叉發表:http://www.coderanch.com/t/565180/GUI/java/turn-off-word-wrap-jeditorpane – camickr

回答

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; 
} 
+0

沒有爲我工作。 – Qix

+0

本示例中鏈接到的頁面的示例通常會起作用,但當您輸入超出邊界的新文本時,將刪除插入符號。 (即一旦滾動條出現)。 – Amber

-1

你爲什麼不使用一個JTextField?

它只有一行。

1

如果你能控制其進入文本,並且您使用的JEditorPane的功能,您可以通過標記HTML代碼,並使用空格: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