getParent() will get the parent container. With this method you will need to do some kind of casting. A better option would be for the menu bar to use an interface that includes the close method.
Here is an example:
JFrame frame = new JFrame(); JMenuBar bar = new JMenuBar(); frame.setJMenuBar(bar); if(bar.getParent().getParent().getParent() instanceof JFrame){ System.out.println(bar.getParent().getParent().getParent()); }
source share