Search code examples
javaswingjmenuitem

Listner for JRadioButtonMenuItem to get the event on disposal


I have a class extending JRadioButtonMenuItem. Is there a way that I can capture an event when the instance of my class is getting disposed from the UI. I am not sure which listener to have to capture this event.

To put it more clearly, I have a set of JRadioButtonMenuItem instance(actually instance of a class that extends JRadioButtonMenuItem), as per the implementation that i have the instance is added as listener to one of my class,say EventSourceEx and listening. This will be added as a listener when the instance of JRadioButtonMenuItem is getting created.

So when the JMenu containing the menu item instance mentioned above,disappears I want to remove the JRadioButtonMenuItem from EventSourceExinstance. So my idea is if there is an even that I can capture from the JRadioButtonMenuItem or the JMenu, I'll be notified that the menu/Menuitem is getting disposed so that I'll remove the instance from the EventSourceEx. Does JMenu or JRadioButtonMenuItem has any mechanism to notify an even when it dissapears.


Solution

  • add property changed listener to your JMenuItem Component.

    Override propertyChange() method

    public void propertyChange(PropertyChangeEvent theEvt)

    {

    if ( !isShowing() && "ancestor".equalsIgnoreCase(theEvt.getPropertyName()) && theEvt.getNewValue() == null)

     {
    
     // Youcan remove your listener
    
     }
    

    }

    try this one... and let me know