Hello all
Does Caret Event only for one component only ?
I tried to find example for more than one TextField which
added with CaretListener,but cannot find one.
Anybody know website or has example like I need ?
I need to know how to detect which component arise the CaretEvent ?

thank you

Recommended Answers

All 4 Replies

that same as if you implements e.g. ActionListener

hello mKorbel
I tried ,but give me error

public static void main(String[] a) {
    JFrame frame = new JFrame();
    JTextField textField = new JTextField();
	textField.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {

    frame.add(new JScrollPane(textField));

    frame.setSize(300, 200);

If you have one listener for multiple text fields then you can get the source of the event from the CaretEvent that's passed as a parameter to your caretUpdate method, eg

public void caretUpdate(CaretEvent e) {
   if (e.getSource() == myFirstTextField) { ...
   else if ((e.getSource() == mySecondTextField) { ...

thank you for both of you mKorbel and James


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.