Java JTextArea中我想实时获取光标在文本框中的行和列

1.用什么监听器监听JTextArea?
2.如何获取光标在文本框的实时位置?

第1个回答  推荐于2016-11-08
public class NewClass {

public static void main(String[] args) {
JFrame frame = new JFrame();
final JTextArea text = new JTextArea();
frame.setDefaultCloseOperation(3);
frame.setContentPane(text);
text.addCaretListener(new CaretListener() {

public void caretUpdate(CaretEvent e) {
System.out.println(e.getMark());
try {
System.out.println(text.modelToView(text.getCaretPosition()));
} catch (BadLocationException ex) {
}
}
});
frame.setSize(500, 600);
frame.setVisible(true);
}
}本回答被提问者采纳
第2个回答  2011-03-31
CaretListener and CaretEvent