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
denny

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();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextField textField = new JTextField();
	textField.setActionCommand("testing");
	
	textField.addCaretListener(new CaretListener() {
	if("testing".equals(e.getActionCommand()))
	{
    public void caretUpdate(CaretEvent e) {
    System.out.println(e);
	}
    }
    });

    frame.add(new JScrollPane(textField));

    frame.setSize(300, 200);
    frame.setVisible(true);
  }

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

regard
denny

Be a part of the DaniWeb community

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