Question

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.

Was it helpful?

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

OTHER TIPS

Instead of trying to capture this event. Focus on capturing the event that you care about that is disposing the container that the button is on. For example if the button is on a frame that is getting closed then add a WindowListener. If the button is removed programatically and re-validated add a call into that code to the logic that you need to be executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top