Java Reference
In-Depth Information
In the case of the CaretListener , there's a single method defined by the interface:
public interface CaretListener implements EventListener {
public void caretUpdate (CaretEvent caretEvent);
}
When the listener is notified, a CaretEvent is sent, which reports on the new dot and mark
locations.
public abstract class CaretEvent extends EventObject {
public CaretEvent(Object source);
public abstract int getDot();
public abstract int getMark();
}
To demonstrate, Figure 15-11 shows a program with a CaretListener attached to the inner
JTextArea . When the CaretEvent happens, the current dot value is sent to the top text field and
the current mark setting is sent to the button. In the example, the cursor dot is at the beginning
of the second line, with the mark at the end.
Figure 15-11. CaretListener sample
Listing 15-10 shows the source associated with the example in Figure 15-11.
Listing 15-10. Listening for Caret Changes
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class CaretSample {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {
JFrame frame = new JFrame("Caret Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane(textArea);
frame.add(scrollPane, BorderLayout.CENTER);
 
Search WWH ::




Custom Search