[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Remove a once-off listener

I have the following:

private boolean firstTime = true;

    // only for initial visuals and selections
    this.addComponentListener(new ComponentListener()
    {
      public void componentResized(ComponentEvent e)
      {
      }
      public void componentMoved(ComponentEvent e)
      {
      }
      public void componentHidden(ComponentEvent e)
      {
      }

      public void componentShown(ComponentEvent e)
      {
        if (firstTime)
        {
          //tree.clearSelection();
          firstTime = false;
        }
      }
    });

once the action has been performed once i want to remove the action - but of course i cant do     this.removeActionLi...  from inside the listener itself!!

Regards,
Cathal.
0
cathalmchale
Asked:
cathalmchale
  • 4
1 Solution
 
zzynxSoftware engineerCommented:
     public void componentShown(ComponentEvent e) {
          // Do whatever needed
          Component c = e.getSource();
          c.removeComponentListener(this);
      }
0
 
zzynxSoftware engineerCommented:
in the above replace
     Component c = e.getSource();
by
     Component c = e.getComponent();
0
 
zzynxSoftware engineerCommented:
So,

    this.addComponentListener(new ComponentAdapter() {         // Use an Adapter if the others stay empty

        public void componentShown(ComponentEvent e) {
            // 1. Do the stuff
           
            // 2. Remove the listener
            e.getComponent().removeComponentListener(this);
        }

    });
0
 
cathalmchaleAuthor Commented:
that easy ;-)
0
 
zzynxSoftware engineerCommented:
;°) Life can be easy
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now