【问题标题】:javax.swing.text.BadLocationException when trying to remove text from doc尝试从文档中删除文本时出现 javax.swing.text.BadLocationException
【发布时间】:2015-11-29 12:56:21
【问题描述】:

我正在尝试从两点之间的文档中删除/删除一些文本。 假设我有一个包含文本“1234XS”的文档,我正在尝试删除位于索引 4 和 5 之间的文本“XS”。但是,当我尝试删除它时出现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"

【问题讨论】:

  • 您要在哪里删除它?在 DocumentListener 中?如果是这样,请不要。

标签: java swing text document


【解决方案1】:
remove(position, position + 1)

remove(...) 方法的参数不是startend 偏移量。

参数为startlength。所以代码应该是:

remove(position, 1)

【讨论】:

    猜你喜欢
    • 2010-09-21
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多