Java 使用DocumentFilter后将文本追加到JTextArea
我在使用DocumentFilter后向JTextArea追加文本时遇到问题, 从文件上传文本后,我需要在JTextArea上追加一个字符串,还需要将另一个JFrame的JTextArea中的字符串返回到指定的JTextArea 在添加DocumentFilter.FilterBypass之前,我没有使用DocumentFilter.FilterBypass,所以一切都很顺利。它仍然起作用,但只有在不添加逗号(,)或空格(“”)时才起作用。这与我得到的规格不符 我怎样才能解决这个问题?或者是否有任何算法或实现没有解决这个问题 这是用于过滤长度的insertString代码,只允许使用空格和逗号Java 使用DocumentFilter后将文本追加到JTextArea,java,swing,Java,Swing,我在使用DocumentFilter后向JTextArea追加文本时遇到问题, 从文件上传文本后,我需要在JTextArea上追加一个字符串,还需要将另一个JFrame的JTextArea中的字符串返回到指定的JTextArea 在添加DocumentFilter.FilterBypass之前,我没有使用DocumentFilter.FilterBypass,所以一切都很顺利。它仍然起作用,但只有在不添加逗号(,)或空格(“”)时才起作用。这与我得到的规格不符 我怎样才能解决这个问题?或者是否有
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;
}
public void insertString(FilterBypass fb,int offset,String String,AttributeSet attr)抛出BadLocationException{
//if(string==null | | string.trim().equals(“”)| | string.equals(“,”)
// {
//返回;
// }
如果(是数字(字符串)){
//如果(this.length>0&&fb.getDocument().getLength())+
//string.length()
//>这个长度){
//返回;
// }
如果(fb.getDocument().getLength()+string.length()>this.length | | string.trim().equals(“”| | string.equals(“,”){
this.insertString(fb,offset,string,attr);
}
//if(string==null | | string.trim().equals(“”)||
//字符串。等于(“,”){
//返回;
// }
super.insertString(fb,offset,string,attr);
}
else if(string==null | | string.trim().equals(“”| | string.equals(“,”){
super.insertString(fb,offset,string,attr);
}
}
@凌驾
public void replace(FilterBypass fb、int offset、int length、字符串文本、AttributeSet attrs)引发BadLocationException{
如果(是数字(文本)){
if(this.length>0&&fb.getDocument().getLength()+text.length()>this.length){
返回;
}
super.insertString(fb、偏移量、文本、属性);
}
}
/**
*此方法测试给定文本是否可以表示为数字。这
*方法可以根据具体需要进一步增强。
*
*@param text
*输入文本。
*@return{@code true}如果给定的字符串可以转换为数字;
*否则返回{@code false}。
*/
专用布尔值isNumeric(字符串文本){
如果(text==null | | text.trim().equals(“”| | text.equals(“,”){
返回true;
}
对于(int-iCount=0;iCount
另外两个函数(append from file和append from another frame)我想通过将它们的字符串值附加到JTextArea来实现,JTextArea使用这个函数进行过滤。但是被super.insertString(…)拒绝了。在添加文本之前,您可能需要获取克拉的位置。我不熟悉DocumentFilters,我假设
this.append(“stringzzz”)
方法不可用
你的补偿好像有问题。您可能需要先设置它以获得一个职位,如下所示
至于获取克拉的位置,您可以执行类似于TextPane.getCaretPosition(),
的操作,然后将其传递进来。而不是使用FilterByPass
类似(如我的链接中所建议的)
这里有一个链接可能会有所帮助
如果我有问题,请告诉我:)我不确定我是否真的明白你的问题。如果您希望有一个过滤器,可以粘贴完整的数字或“,”和空白(结束或开始或输入),但不能粘贴任何其他文本,您只需更改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();
}
您目前使用的哪些代码不起作用?介意把它贴出来吗?没问题,不过时间很长。必须附加两个框架的代码可能只是您试图添加“,”和“”的代码片段?但我收到的规范要求我添加它们。我不能摆脱他们,我怎样才能摆脱他们。javadoc具体说明了这一点-1,您对插入符号位置的评论与问题无关。文档(或模型)对Swing组件(视图)一无所知。请记住,一个模型可以由多个组件共享。DocumentFilter用于在文本插入文档之前编辑文本。如果OPs代码不起作用是因为DocumentFilter中的逻辑错误。@camickr说它起作用是有道理的,但只有在没有使用特定字符时,他才让我感到困惑,然后说只要他没有回溯到它,它就起作用。所以这让我相信这是另外一回事。@camickr,你介意我粘贴文档过滤器逻辑的代码让你看一下吗。再多阅读一些javadoc,问题似乎不是来自documentfilter本身,而是AbstractDocument类。在这种情况下,您不需要在insertString方法中进行其他检查感谢这篇文章,我上周一直对这篇文章很好奇,希望这是一个答案。所以只剩下一个小时了,我会给你赏金的。
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();
}