how to remove anonymous listener?

Discussion in 'Java' started by Brandon McCombs, Aug 17, 2006.

  1. 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 = new JCheckBox(strAccountProps);
    cbAccountProps.setFont(fnt2);
    cbAccountProps.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
    Brandon McCombs, Aug 17, 2006
    #1
    1. Advertising

  2. Brandon McCombs

    Eric Sosman Guest

    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 = new JCheckBox(strAccountProps);
    > cbAccountProps.setFont(fnt2);
    > cbAccountProps.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.removeItemListener() later on.

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

    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.removeItemListener(snoops);
    // safe to discard snoops now (if they won't be
    // re-activated later)

    --
    Eric Sosman
    lid
    Eric Sosman, Aug 17, 2006
    #2
    1. Advertising

  3. 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 = new JCheckBox(strAccountProps);
    >> cbAccountProps.setFont(fnt2);
    >> cbAccountProps.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.removeItemListener() later on.
    >
    > ItemListener[] snoops =
    > new ItemListener[cbAccountProps.length];
    > // ...
    > for (int i = 0; i < cbAccountProps.length; i++) {
    > // ...
    > snoops = new ItemListener() {
    > public void itemStateChanged(ItemEvent e) {
    > notifyChanges(hasAccountStatusChanged);
    > }
    > };
    > cbAccountProps.addItemListener(snoops);
    > }
    >
    > 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.removeItemListener(snoops);
    > // safe to discard snoops now (if they won't be
    > // re-activated later)
    >



    thanks Eric. That worked great.
    Brandon McCombs, Aug 17, 2006
    #3
  4. Brandon McCombs

    Guest

    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 = new JCheckBox(strAccountProps);
    > cbAccountProps.setFont(fnt2);
    > cbAccountProps.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
    , Aug 17, 2006
    #4
  5. 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 = new JCheckBox(strAccountProps);
    >> cbAccountProps.setFont(fnt2);
    >> cbAccountProps.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
    Brandon McCombs, Aug 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon-Pierre  Jarry
    Replies:
    2
    Views:
    2,367
    Henrik
    Aug 10, 2005
  2. Replies:
    1
    Views:
    277
    bruce barker \(sqlwork.com\)
    Oct 18, 2006
  3. Reporter
    Replies:
    3
    Views:
    469
    Mike Schilling
    May 12, 2007
  4. Replies:
    1
    Views:
    218
  5. Csaba Gabor

    Removing an anonymous event listener

    Csaba Gabor, Aug 5, 2005, in forum: Javascript
    Replies:
    3
    Views:
    109
    Martin Honnen
    Aug 6, 2005
Loading...

Share This Page