Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to remove anonymous listener?

Reply
Thread Tools

how to remove anonymous listener?

 
 
Brandon McCombs
Guest
Posts: n/a
 
      08-17-2006
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      08-17-2006
Brandon McCombs wrote:

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


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)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Brandon McCombs
Guest
Posts: n/a
 
      08-17-2006
Eric Sosman wrote:
> Brandon McCombs wrote:
>
>> 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.

>
> 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)
>



thanks Eric. That worked great.
 
Reply With Quote
 
dsjoblom@abo.fi
Guest
Posts: n/a
 
      08-17-2006

Brandon McCombs wrote:
> 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.


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

 
Reply With Quote
 
Brandon McCombs
Guest
Posts: n/a
 
      08-18-2006
(E-Mail Removed) wrote:
> Brandon McCombs wrote:
>> 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.

>
> 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
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
Reduce authentication traffic (remove anonymous requests) Oleg.Ogurok@gmail.com ASP .Net 1 10-18-2006 12:27 AM
Httpmodule still executed even if remove using <remove> config entry Simon-Pierre Jarry ASP .Net 2 08-10-2005 11:22 AM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM



Advertisments