I would like to let my JFrame under Windows not act upon an ALT
key press. To clarify, when you execute the following snippet of code:
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
and press the ALT
key and then the arrow down
key, you get a menu in the upper left corner in which you can choose to minimize, move, close etc. the frame (at least I get that). I would like to disable this: ie. the JFrame should not "listen" to these ALT
presses.
I believe that certain Windows components react by default on the ALT
key because when I add a menu bar to my frame, and explicitly set the look & feel to the system look & feel, the menu (File
) is now automatically selected after pressing the ALT
key:
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) throws Exception {
JFrame frame = new JFrame();
frame.setSize(400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
frame.setJMenuBar(menuBar);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); // set Windows look and feel
frame.setVisible(true);
}
}
and when I remove UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
from the example above, this behaviour is not exhibited when pressing the ALT
key: the menu is not selected, but the JFrame is.
When no look & feel is set, the "Metal" look and feel is used. It is clear by looking at the menu bar in my previous example that you go from "native look" to "Metal look" when you remove UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
from the code. However, I don't see a change in the JFrame, no matter what look & feel I set, it always looks like a native Windows frame.
So, my question is: how can I disable this ALT
behaviour on my JFrame? I guess I can do it by actually changing the look and feel of the JFrame. Is that possible? If so, how?
Thanks!