Is there a way to get the Window which a JMenuItem belongs to? I tried this proof of-concept:
JFrame testFrame = new JFrame();
testFrame.setSize(300, 100);
testFrame.setLocationRelativeTo(null);
JMenuBar testBar = new JMenuBar();
JMenu testMenu = new JMenu("Test");
JMenuItem testItem = new JMenuItem("Parent Test");
testItem.addActionListener(e -> {
System.out.println("Has Top Level Ancestor: " + (testItem.getTopLevelAncestor() != null));
System.out.println("Has Window Ancestor: " + (SwingUtilities.getWindowAncestor(testItem) != null));
});
testBar.add(testMenu);
testMenu.add(testItem);
testFrame.setJMenuBar(testBar);
testFrame.setVisible(true);
The output was:
Has Top Level Ancestor: false
Has Window Ancestor: false