Java Swing How to - Override menu selection change method to listen to Its Selection Status








Question

We would like to know how to override menu selection change method to listen to Its Selection Status.

Answer

/* w  ww.ja  v a  2s .  c  o  m*/
import javax.swing.JMenuItem;

public class Main {
  public static void main(String[] argv) throws Exception {
    JMenuItem item = new JMenuItem("Label") {
      public void menuSelectionChanged(boolean isSelected) {
        super.menuSelectionChanged(isSelected);
        if (isSelected) {
          System.out.println("The menu item is selected"); 
        } else {
          System.out.println("The menu item is no longer selected"); 
        }
      }
    };
  }
}