Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Inline Listeners

Thread view: 
Jason Cavett - 11 Jun 2007 15:25 GMT
I am added listeners (DocumentListener) to a GUI component
(specifically a class that extends JTextField).  I am wondernig why
listeners can be instantiated WITHOUT knowing any of the information
within the listener.  For example...

           nameTextField.getDocument().addDocumentListener(
                   new DocumentListener() {
                       public void changedUpdate(DocumentEvent e) {
                           if (fireEvent) {
                               update();
                           }
                       }

                       public void insertUpdate(DocumentEvent e) {
                           if (fireEvent) {
                               update();
                           }
                       }

                       public void removeUpdate(DocumentEvent e) {
                           if (fireEvent) {
                               update();
                           }
                       }

                       private void update() {

dataModel.setName(nameTextField.getText());
                           commonUpdate();
                           nameTextField.verify();
                       }
                   });

This listener is added when the GUI is instantiated, however, it is
not until later that I set the dataModel.  Why does this work?  (I
realize this is probably a fundamental question, but since I never
understood it fully, I figured I would ask.)
Robert Klemme - 11 Jun 2007 15:43 GMT
> I am added listeners (DocumentListener) to a GUI component
> (specifically a class that extends JTextField).  I am wondernig why
[quoted text clipped - 33 lines]
> realize this is probably a fundamental question, but since I never
> understood it fully, I figured I would ask.)

Your anonymous class inherits a reference to the instance of the
surrounding class.  Your method commonUpdate() are invoked on that
instance; same for fields ("nameTextField" in this case).

Kind regards

    robert
Jason Cavett - 11 Jun 2007 17:37 GMT
> > I am added listeners (DocumentListener) to a GUI component
> > (specifically a class that extends JTextField).  I am wondernig why
[quoted text clipped - 43 lines]
>
> - Show quoted text -

Cool.  Thank you.

Is there any particular reason you would/would not want to do this?
(For example, the reason I did it this way is because every text field
reacts somewhat differently depending on the field.
Arne Vajhøj - 12 Jun 2007 01:31 GMT
>>> I am added listeners (DocumentListener) to a GUI component
>>> (specifically a class that extends JTextField).  I am wondernig why
>>> listeners can be instantiated WITHOUT knowing any of the information
>>> within the listener.  For example...

>> Your anonymous class inherits a reference to the instance of the
>> surrounding class.  Your method commonUpdate() are invoked on that
>> instance; same for fields ("nameTextField" in this case).

> Is there any particular reason you would/would not want to do this?
> (For example, the reason I did it this way is because every text field
> reacts somewhat differently depending on the field.

Some people (including me) find the code rather unreadable.

Arne
Jason Cavett - 12 Jun 2007 19:17 GMT
On Jun 11, 8:31 pm, Arne Vajh?j <a...@vajhoej.dk> wrote:
> >>> I am added listeners (DocumentListener) to a GUI component
> >>> (specifically a class that extends JTextField).  I am wondernig why
[quoted text clipped - 10 lines]
>
> Arne

Yeah.  That's my biggest complaint.  But, honestly, I'm not sure of a
better way to go about it.  I mean, given the fact that something
different is done based on each text field, button, etc it seems like
I could either:

A. Create a ton of specific classes for each item.
B. Do it inline.
C. ???

If there's another way, I'd be open to hearing it.  Listeners have
always given me problems from an OO perspective.
Arne Vajhøj - 13 Jun 2007 04:30 GMT
>>>>> I am added listeners (DocumentListener) to a GUI component
>>>>> (specifically a class that extends JTextField).  I am wondernig why
[quoted text clipped - 18 lines]
> If there's another way, I'd be open to hearing it.  Listeners have
> always given me problems from an OO perspective.

I am not aware of any brilliant solution.

I tend to prefer passing this to all and then have
the actionPerformed test for source.

Arne


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.