【问题标题】:Appending a text to a JTextArea after Using a DocumentFilter使用 DocumentFilter 后将文本附加到 JTextArea
【发布时间】:2011-09-20 11:30:42
【问题描述】:

我在使用 DocumentFilter 后将文本附加到 JTextArea 时遇到问题, 从文件上传文本后,我需要在 JTextArea 上附加一个字符串,并将另一个 JFrame 的 JTextArea 中的字符串返回到指定的 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(.....) 拒绝

【问题讨论】:

  • 您目前使用的哪些代码不起作用?介意张贴吗?
  • 没问题,但它很长。必须附上两个框架的代码
  • 也许只是您尝试添加“,”和“”的sn-p?
  • 但是给我的规范要求我添加它们。我无法摆脱它们
  • 我怎样才能绕过它。 javadoc 完全是关于这样做的

标签: java swing


【解决方案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();
}

【讨论】:

  • 在这种情况下,您不需要在 insertString 方法中进行其他检查
  • 感谢您的帖子,上周我一直对这篇帖子感到好奇,希望这是一个答案。所以只剩下一个小时,我将奖励你赏金。
【解决方案2】:

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

您的偏移量似乎有问题。您可能需要先设置它以获得一个位置,如下所示。 InsertString() Doc (OffSet)

至于获得克拉位置,您可以执行TextPane.getCaretPosition(), 之类的操作并将其传入。而不是使用FilterByPass?

类似的东西(如我的链接中所建议的)

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

这是一个可能有帮助的链接。

Inserting Text without FilterByPass

如果我离开了,请告诉我:)

【讨论】:

  • -1,您关于插入符号位置的cmets与问题无关。文档(或模型)对 Swing 组件(视图)一无所知。记住一个模型可以被多个组件共享。 DocumentFilter 用于在将文本插入文档之前对其进行编辑。如果 OPs 代码不工作是因为 DocumentFilter 中的逻辑错误。
  • @camickr 有道理,当他说它有效但仅在不使用特定字符时让我感到困惑,然后说只要他不回溯它就有效。所以这让我相信这是另一回事。
  • @camickr,你介意吗,如果我粘贴文档过滤器逻辑的代码供你查看。多阅读 javadoc,看来问题不在于 documentfilter 本身,而在于 AbstractDocument 类。
猜你喜欢
  • 1970-01-01
  • 2023-03-08
  • 2015-04-03
  • 1970-01-01
  • 2015-09-04
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多