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 / August 2006

Tip: Looking for answers? Try searching our database.

how to remove anonymous listener?

Thread view: 
Brandon McCombs - 17 Aug 2006 02:59 GMT
I setup an array of combo boxes using the following (some code removed
such as layout properties):

for (int i = 0; i < cbAccountProps.length; i++) {
    cbAccountProps[i] = new JCheckBox(strAccountProps[i]);
    cbAccountProps[i].setFont(fnt2);
    cbAccountProps[i].addItemListener( new ItemListener() {
        public void itemStateChanged(ItemEvent e) {
            notifyChanges(hasAccountStatusChanged);
        }
    });
}

When I initialize the dialog window that this code is in the window is
automatically set to show that changes have been made to the user whose
properties are displayed in the dialog. This is because the data is set
while the listeners are active.  I already have 2 methods that go
through disabling/enabling all document listeners (for a bunch of
textfields that are also in the dialog window). But I'm not sure how to
remove/add the anonymous inner class above in those same methods so that
when I set data upon the dialog opening it hasn't yet detected changes
programmatically, let alone by the operator of the application. I have a
feeling I will have to have a separate ItemListener class setup so that
I can declare an instance of it.

thanks
Eric Sosman - 17 Aug 2006 03:09 GMT
> I setup an array of combo boxes using the following (some code removed
> such as layout properties):
[quoted text clipped - 20 lines]
> feeling I will have to have a separate ItemListener class setup so that
> I can declare an instance of it.

    You can still have an anonymous class, but you need to
remember a reference to the instance so you can pass it to
cbAccountProps[i].removeItemListener() later on.

    ItemListener[] snoops =
       new ItemListener[cbAccountProps.length];
    // ...
    for (int i = 0; i < cbAccountProps.length; i++) {
       // ...
       snoops[i] = new ItemListener() {
           public void itemStateChanged(ItemEvent e) {
               notifyChanges(hasAccountStatusChanged);
           }
       };
       cbAccountProps[i].addItemListener(snoops[i]);
    }

    When you want to remove them, use the array that you've
carefully remembered to do

    for (int i = 0;  i < cbAccountProps.length;  ++i)
       cpAccountProps[i].removeItemListener(snoops[i]);
    // safe to discard snoops now (if they won't be
    // re-activated later)

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Brandon McCombs - 17 Aug 2006 03:25 GMT
>> I setup an array of combo boxes using the following (some code removed
>> such as layout properties):
[quoted text clipped - 45 lines]
>     // safe to discard snoops now (if they won't be
>     // re-activated later)

thanks Eric. That worked great.
dsjoblom@abo.fi - 17 Aug 2006 17:27 GMT
> I setup an array of combo boxes using the following (some code removed
> such as layout properties):
[quoted text clipped - 20 lines]
> feeling I will have to have a separate ItemListener class setup so that
> I can declare an instance of it.

Although Eric already gave you a solution to your problem, it doesn't
seem to be necessary to remove and readd the listeners all the time.
You could just keep a boolean variable that specifies whether the user
or the app is changing the state of the components, and modify the
notifyChanges method to take into account the state of the variable. Or
even simpler, after loading data into the dialog after it is opened
just do the reverse of notifyChanges (assuming it is easier to do so
than to remove and readd all listeners.)

Also, you don't need to create a separate listener for each checkbox if
all of the listeners do the same thing. You can just add a single
listener to all checkboxes.

Regards,
Daniel Sjöblom
Brandon McCombs - 18 Aug 2006 05:39 GMT
>> I setup an array of combo boxes using the following (some code removed
>> such as layout properties):
[quoted text clipped - 36 lines]
> Regards,
> Daniel Sjöblom

thanks Daniel, I hadn't thought of just making a single listener since
they do in fact all do the same thing. I just created a class after all
and passed that to the addItemListener() for each checkbox.  I'll also
look into the idea of reversing the notifyChanges() method. It doesn't
look to be too difficult and it will be shorter than having a list of
addDocumentListener() and removeDocumentListener().

thanks for the input


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



©2008 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.