Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

adding a event listener only once

I am adding a event listener to the singlteton class.  But I am only wanting to add the listener one time.  Right now I have it in a add method.

 public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
      String revMasterForm;
    ***  tcRevision.getSession().addAIFComponentEventListener(this); ***
      
      try {
         revMasterForm = tcRevision.getRelatedComponent("IMAN_master_form_rev").getUid();
         RevDataset pp = new RevDataset(tcRevision, selectedDataset, revMasterForm);
         if (!rds.contains(pp)) {
            rds.add(pp);
         }     
      }
      catch (TCException e) {
        
         e.printStackTrace();
      }
     
      fireTableDataChanged();
   }// end add()

Open in new window


I don't want to add the listener everything a object is added to the array.  How would I only add the listener once?
Avatar of jkteater
jkteater
Flag of United States of America image

ASKER

Maybe the  question should be how to do check and see if the listener is already running.

Something like

If (Listener value == nul ) {
tcRevision.getSession().addAIFComponentEventListener(this);
}

not sure how to get the listener value
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
where would you add the listener?
Ah well, that's just a guess based on instinct ;) Not knowing your setup, i couldn't begin to say. You should probably ignore that comment of mine ;)
I basically just created a boolean to check

if(!addedListener){
         tcRevision.getSession().addAIFComponentEventListener(this);
         addedListener = true;
      }

Open in new window


Seems to work
So only one listener is possible?
Yes
Well if it can contain only one listener, then a boolean will suffice. However, that listener should be accessible to clients via a standard accessor method along the lines of getListener(). For that reason, no extra flag is necessary. In fact, an extra flag is error-prone as well as redundant. All you need is something like i posted in my last code illustration
:)