Listening for Changes to the Currently Selected Menu or Menu Item
import javax.swing.MenuElement; import javax.swing.MenuSelectionManager; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; public class Main { public static void main(String[] argv) throws Exception { MenuSelectionManager.defaultManager().addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent evt) { MenuSelectionManager msm = (MenuSelectionManager) evt.getSource(); MenuElement[] path = msm.getSelectedPath(); if (path.length == 0) { } } }); } }