I am trying to maximize JFrame from within JMenuBar, I can not pass a reference to the frame. Is it possible to get a reference to the frame that it is used in?
i can get to the top level component but it does not have a way to maximize and minimize frame.
public Container getApplicationFrame(ActionEvent event){
JMenuItem menuItem = (JMenuItem) event.getSource();
JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
Component invoker = popupMenu.getInvoker();
JComponent invokerAsJComponent = (JComponent) invoker;
Container topLevel = invokerAsJComponent.getTopLevelAncestor();
return topLevel;
}
You can get the Window that contains the JPanel via
You can then either maximise it using
window.setSize()— or, since you seem to know that it’s a JFrame, cast it to Frame and use thesetExtendedStatemethod that Kevin mentions. Example code from the Java Developers’ Almanac for that: