Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.awt.Component;

import javax.swing.JMenuItem;
import javax.swing.MenuElement;
import javax.swing.MenuSelectionManager;

public class Main {
    public static void main(String[] argv) throws Exception {
        MenuElement[] path = MenuSelectionManager.defaultManager().getSelectedPath();

        if (path.length == 0) {
            System.out.println("No menus are opened or menu items selected");
        }
        for (int i = 0; i < path.length; i++) {
            Component c = path[i].getComponent();

            if (c instanceof JMenuItem) {
                JMenuItem mi = (JMenuItem) c;
                String label = mi.getText();
            }
        }
    }
}