Hi all

in code below ,after I got the object aobj ,how can I identify which
component aobj actualy is.And where is it Location ?

thank you
denny

CaretListener listener = new CaretListener() {
      public void caretUpdate(CaretEvent caretEvent) {
       
		Object aobj=caretEvent.getSource();
		Component comp=aobj.
		System.out.println(aobj.toString());
      }
    };

Recommended Answers

All 2 Replies

You can compare aobj with the possible JComponents that it could be, eg

if (aobj == myJTextField1) ...
else (aobj == myJTextField2) ...

When you know what the source is, you can cast aobj to the appropriate class, eg

if (aobj == myJTextField1) JTextField tf = (JTextField) aobj

or maybe all the possible sources are JTextFields anyway so you can start with

JTextField source = (JTextField) caretEvent.getSource();

Finally, depending on how many components share this listener, and how similar or dissimilar their processing is, it may be best to have a different listener for each component so you don't need to check what the source was.

Thank James

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.