0

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());
      }
    };
2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by dennysimon
1

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.