请问前辈们,JTextArea能在检测输入的事件处理中同时插入字符到文本域吗?
我这样操作老是报错:Invalid location
textArea.addCaretListener(new CaretListener() {
@Override
public void caretUpdate(CaretEvent e) {
String temp = null;
int tip = e.getDot();
String code = null; //
System.out.println("tip = "+tip);
try {
code = textArea.getText(e.getDot()-1, 1);
} catch (BadLocationException e2) {
e2.printStackTrace();
}
if (code.equals("{") && tip > nowTip) {
textArea.insert("\n\t\n}", tip);
}
if (code.equals("(") && tip > nowTip) {
AddText2 at = new AddText2();
at.start();
}
nowTip = e.getDot();
}
});