Reputation:
I want to create a JMenuItem
which has an Accelerator of shift + f11
.
by pressing shift + f11
or clicking on JMenuItem
it must get fullScreen.
does anyone have any advice?
JMenuItem toggle_full_screenFull = new JMenuItem("Toggle Full Screen");
toggle_full_screenFull.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11 , InputEvent.SHIFT_DOWN_MASK));
Upvotes: 1
Views: 81
Reputation: 6808
The following code works for me:
public class FullScreenExample extends JFrame {
public FullScreenExample() {
super("");
JMenuBar menuBar = new JMenuBar();
JMenu homeMenu = new JMenu("home");
JMenuItem fullScreen = new JMenuItem("full screen");
fullScreen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F11, KeyEvent.SHIFT_MASK));
fullScreen.addActionListener(e->setExtendedState(JFrame.MAXIMIZED_BOTH));
homeMenu.add(fullScreen);
menuBar.add(homeMenu);
setJMenuBar(menuBar);
setLocationByPlatform(true);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new FullScreenExample().setVisible(true));
}
}
And it works either with KeyEvent.SHIFT_DOWN_MASK
or KeyEvent.SHIFT_MASK
.
Now, if you want to make it function like enable/disable full screen mode:
fullScreen.addActionListener(e -> {
boolean isNormal = getExtendedState() == JFrame.NORMAL;
setExtendedState(isNormal ? JFrame.MAXIMIZED_BOTH : JFrame.NORMAL);
});
Upvotes: 1