-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.4.1
-
x86
-
windows_xp
Name: rv122619 Date: 08/20/2004
One of our products has demonstrated non-standard behaviour whereby a modal dialog that has no focusable components does not block key events and therefore allows menubar accelerators to run.
Current Behavior:
If you have a Modal Window open and try to access a menu bar item, via the keyboard(ALT + mnemonic) computer does not beep at user.
Beep, does occur with a mouse.
Expected Behavior:
When you try to access a menu bar item via keyboard with a Modal Window open, access should not be allowed and a beep should be provided to user.
Also, if you have a dialog with nothing in it to receive input focus (which is probably never the case), the modal dialog doesn't lock one out.
Source code that demonstrates issue and provides workaround:
import java.awt.BorderLayout;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.awt.MenuShortcut;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;
public class TestFrame
extends JFrame
{
public static void main(String[] args)
{
boolean awtMenuBar = (args != null && args.length > 0 && args[0].equals("awt"));
TestFrame frame = new TestFrame(awtMenuBar);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public TestFrame(boolean useAWTMenuBar)
{
setTitle("Test Frame");
getContentPane().add(new JLabel("Test Frame"));
if (useAWTMenuBar)
setMenuBar(createAWTMenuBar());
else
setJMenuBar(createJMenuBar());
}
private MenuBar createAWTMenuBar()
{
MenuBar menuBar = new MenuBar();
Menu menu = new Menu("File");
menuBar.add(menu);
MenuItem mi = new MenuItem("Show Focusable Dialog");
mi.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showTestDialog(TestFrame.this, true);
}
});
menu.add(mi);
mi = new MenuItem("Show Non-Focusable Dialog");
mi.setShortcut(new MenuShortcut(KeyEvent.VK_D, false));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showTestDialog(TestFrame.this, false);
}
});
menu.add(mi);
return menuBar;
}
private JMenuBar createJMenuBar()
{
JMenuBar menuBar = new JMenuBar();
JMenu menu = new JMenu("File");
menuBar.add(menu);
JMenuItem mi = new JMenuItem("Show Focusable Dialog");
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showTestDialog(TestFrame.this, true);
}
});
menu.add(mi);
mi = new JMenuItem("Show Non-Focusable Dialog");
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK));
mi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
showTestDialog(TestFrame.this, false);
}
});
menu.add(mi);
return menuBar;
}
public static void showTestDialog(JFrame frame, boolean addFocusableComp)
{
final JDialog dialog = new JDialog(frame, "Test Dialog", true);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
if (addFocusableComp)
{
dialog.getContentPane().add(new JLabel("Test dialog with a focusable component."), BorderLayout.NORTH);
dialog.getContentPane().add(new JButton("A Focusable Button"));
}
else
{
dialog.getContentPane().add(new JLabel("Test dialog with no focusable components."));
// // workaround 1
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// dialog.getContentPane().requestFocus();
// }
// });
// // workaround 2
// dialog.addKeyListener(new java.awt.event.KeyAdapter() {
// public void keyPressed(java.awt.event.KeyEvent event) {
// event.consume();
// }
// public void keyReleased(java.awt.event.KeyEvent event) {
// event.consume();
// }
// public void keyTyped(java.awt.event.KeyEvent event) {
// event.consume();
// }
// });
}
dialog.pack();
dialog.setLocationRelativeTo(frame);
dialog.setVisible(true);
}
}
If run normally (i.e. "java TestFrame") you will be able to bring up multiple non-focusable modal dialogs (using ctrl-D). If you pass "awt" as an argument, the code will use an AWT menubar instead of a Swing menubar and this problem doesn't occur.
======================================================================
- duplicates
-
JDK-5043506 JRootPane only gets key events if content pane contains input fields
-
- Closed
-