Duplicate :
|
|
Relates :
|
|
Relates :
|
Name: rv122619 Date: 08/20/2004 If you have a modal dialog that contains focusable components (i.e. a message box using JOptionPane) that is displayed, you can use alt+tab to navigate to the parent window. The parent window becomes active. If you were to try and click on the parent window, the message box is re-activated. Instead of clicking on the parent window if you were to use the arrow keys you could navigate the menu or if you were to use the accelerators you could perform the associated actions getting around the modal blocking. 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); } } To see the problem bring up a focusable modal dialog (using ctrl-S). Try to bring up another and you can't. Using Alt-Tab navigate to the parent window and then try ctrl-S. You will be able to bring up another focusable modal dialog. ======================================================================
|