尝试从文档中删除文本时发生javax.swing.text.BadLocationException
我正试图在两点之间从文档中删除一些文本。 假设我有一个文本为“1234XS”的文档,我试图删除文本“XS”,它位于索引4和5之间。然而,当我尝试删除它时,我得到了尝试从文档中删除文本时发生javax.swing.text.BadLocationException,java,swing,text,document,Java,Swing,Text,Document,我正试图在两点之间从文档中删除一些文本。 假设我有一个文本为“1234XS”的文档,我试图删除文本“XS”,它位于索引4和5之间。然而,当我尝试删除它时,我得到了BadLocationException错误。以下是我所拥有的: System.out.println(tp.getText().length());//tp is a JTextPane. prints out 6, just to show I'm not going out of bounds System.out.println
BadLocationException
错误。以下是我所拥有的:
System.out.println(tp.getText().length());//tp is a JTextPane. prints out 6, just to show I'm not going out of bounds
System.out.println(position+ "-" + (position+ 1));//prints out 4 and 5
tp.getStyledDocument().remove(position, (position + 1));//crashes here, trying to remove "XS" from "1234XS"
remove(…)
方法的参数不是start
和end
偏移量
参数是开始
和长度
。因此,代码应该是:
remove(position, 1)
remove(…)
方法的参数不是start
和end
偏移量
参数是开始
和长度
。因此,代码应该是:
remove(position, 1)
你想从哪里删除这个?在DocumentListener中?如果是,请不要。您要从何处删除此项?在DocumentListener中?如果是这样,不要。