Created
September 14, 2010 10:41
-
-
Save tyano/578848 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import javax.swing.JFrame; | |
import javax.swing.JTextArea; | |
import javax.swing.event.DocumentEvent; | |
import javax.swing.event.DocumentListener; | |
import javax.swing.text.DefaultCaret; | |
import javax.swing.SwingUtilities; | |
public class Sample extends JFrame { | |
private JTextArea area; | |
public Sample() { | |
super(); | |
area = new JTextArea(); | |
area.getDocument().addDocumentListener(new DocumentListenerImpl()); | |
setLayout(new BorderLayout()); | |
add(area); | |
setSize(400, 300); | |
setVisible(true); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
} | |
public static void main(String[] args) { | |
Sample sample = new Sample(); | |
sample.setVisible(true); | |
} | |
class DocumentListenerImpl implements DocumentListener { | |
public void changedUpdate(DocumentEvent e) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
System.out.println(area.getCaret().getMagicCaretPosition()); | |
} | |
}); | |
} | |
public void insertUpdate(DocumentEvent e) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
System.out.println(area.getCaret().getMagicCaretPosition()); | |
} | |
}); | |
} | |
public void removeUpdate(DocumentEvent e) { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
System.out.println(area.getCaret().getMagicCaretPosition()); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment