2011-09-20 45 views
5

我在使用DocumentFilter後將文本附加到JTextArea存在問題, 我需要在從文件上傳文本後在JTextArea上附加字符串,也返回從另一個JFrame的JTextArea到指定的JTextArea的字符串在使用DocumentFilter後將文本附加到JTextArea

當我沒有使用DocumentFilter.FilterBypass,直到添加它時,所有工作都很完美。它仍然有效,但只有當逗號(,)或空格(「」)未被添加時。這不符合我給出的規範。

我該如何解決這個問題?或者是否有任何算法或實現不能解決這個問題?

這是insertString代碼過濾長度,並且只允許空間和逗號

public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { 
    // if (string == null || string.trim().equals("") || string.equals(",")) 
    // { 
    // return; 
    // } 

    if (isNumeric(string)) { 
     // if (this.length > 0 && fb.getDocument().getLength() + 
     // string.length() 
     // > this.length) { 
     // return; 
     // } 
     if (fb.getDocument().getLength() + string.length() > this.length || string.trim().equals("") || string.equals(",")) { 
      this.insertString(fb, offset, string, attr); 
     } 
     // if (string == null || string.trim().equals("") || 
     // string.equals(",")) { 
     // return; 
     // } 
     super.insertString(fb, offset, string, attr); 
    } 
    else if (string == null || string.trim().equals("") || string.equals(",")) { 
     super.insertString(fb, offset, string, attr); 
    } 

} 

@Override 
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException { 
    if (isNumeric(text)) { 
     if (this.length > 0 && fb.getDocument().getLength() + text.length() > this.length) { 
      return; 
     } 
     super.insertString(fb, offset, text, attrs); 
    } 
} 

/** 
* This method tests whether given text can be represented as number. This 
* method can be enhanced further for specific needs. 
* 
* @param text 
*   Input text. 
* @return {@code true} if given string can be converted to number; 
*   otherwise returns {@code false}. 
*/ 
private boolean isNumeric(String text) { 
    if (text == null || text.trim().equals("") || text.equals(",")) { 
     return true; 
    } 
    for (int iCount = 0; iCount < text.length(); iCount++) { 
     if (!Character.isDigit(text.charAt(iCount))) { 
      return false; 
     } 
    } 
    return true; 
} 

其他兩個函數(從文件&追加來自不同幀附加)欲無辜實現僅通過追加他們的字符串值使用此過濾到JTextArea。但被super.insertString(.....)拒絕

+0

什麼代碼是你在目前不工作的時候使用?介意發佈它? – sealz

+0

沒問題,但它很長。必須附上兩個框架的代碼 – nnanna

+0

也許只是您要添加「,」和「」的片段? – sealz

回答

1

我不知道我真的得到了你的問題。如果你想有一個過濾器,您可以粘貼完整的數字或「」和空格(結束或開始或進入),但不能粘貼任何其他文本你可以改變你的IsNumeric函數:

private boolean isNumeric(String text) { 
    text = text.trim(); 
    if(",".equals(text)) return true; 
    ParsePosition position = new ParsePosition(0); 
    java.text.NumberFormat.getNumberInstance().parse(text, position); 
    return position.getIndex() == text.length(); 
} 
+0

在這種情況下,你不需要在你的insertString方法中進行其他檢查。 – Christian

+0

感謝這篇文章,上週我一直對這篇文章感到好奇,希望這是一個答案。所以只剩下一個小時我就會獎賞你的賞金。 – sealz

+0

哇,謝謝!我希望它能真正解決你的問題。 – Christian

0

您可能需要在追加文本之前獲取克拉位置。我不熟悉DocumentFilters,我假設this.append("stringzzz")方法不可用?

看來你的偏移有問題。您可能需要將其設置爲首先獲得某個職位,如下所示。 InsertString() Doc (OffSet)

至於獲得克拉位置,你可以做一些類似於TextPane.getCaretPosition(),的東西,並將其傳入。而不是使用FilterByPass?

喜歡的東西(在我的鏈接建議)

this.insertString(TextArea.getCaretPosition(), yourString, null); 

下面是可能會有所幫助的鏈接。

Inserting Text without FilterByPass

讓我知道如果我的路要走:)

+0

-1,您對插入位置的評論與問題無關。文檔(或模型)對Swing組件(視圖)一無所知。記住一個模型可以被多個組件共享。 DocumentFilter用於在將文本插入到Document中之前對其進行編輯。如果由於DocumentFilter中的邏輯錯誤而導致OP代碼無法工作。 – camickr

+0

@camickr當他說它有效時,他讓我感到困惑,但只有在不使用特定人物時纔會感到困惑,然後說,只要他不回溯到它,它就會工作。所以這讓我相信這是另一回事。 – sealz

+0

@camickr,你介意,如果我粘貼的代碼爲documentfilter的邏輯讓你看看。再閱讀一下javadoc,看起來問題不在於documentfilter本身,而在於AbstractDocument類。 – nnanna