45 private static JPanel panel; 46 private static JComboBox<String> combo1; 47 private static JComboBox<String> combo2; 48 private static JMenuBar menubar; 49 private static JMenu menu1; 50 private static JMenu menu2; 51 private static Rectangle fullScreenBounds; 52 // The usable desktop space: screen size - screen insets. 53 private static Rectangle screenBounds; 54 private static String[] numData = { 55 "One", "Two", "Three", "Four", "Five", "Six", "Seven" 56 }; 57 private static String[] dayData = { 58 "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" 59 }; 60 private static char[] mnDayData = { 61 'M', 'T', 'W', 'R', 'F', 'S', 'U' 62 }; 63 64 public TaskbarPositionTest() { 65 super("Use CTRL-down to show a JPopupMenu"); 66 setContentPane(panel = createContentPane()); 67 setJMenuBar(createMenuBar("1 - First Menu", true)); 68 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 69 70 // CTRL-down will show the popup. 71 panel.getInputMap().put(KeyStroke.getKeyStroke( 72 KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP"); 73 panel.getActionMap().put("OPEN_POPUP", new PopupHandler()); 74 75 pack(); 76 77 Toolkit toolkit = Toolkit.getDefaultToolkit(); 78 fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); 79 screenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); 80 81 // Place the frame near the bottom. This is a pretty wild guess. 82 this.setLocation(0, (int) screenBounds.getHeight() - 2 * this.getHeight()); 83 84 // Reduce the screen bounds by the insets. 85 GraphicsConfiguration gc = this.getGraphicsConfiguration(); 86 if (gc != null) { 87 Insets screenInsets = toolkit.getScreenInsets(gc); 88 screenBounds = gc.getBounds(); 89 screenBounds.width -= (screenInsets.left + screenInsets.right); 90 screenBounds.height -= (screenInsets.top + screenInsets.bottom); 91 screenBounds.x += screenInsets.left; 92 screenBounds.y += screenInsets.top; 95 setVisible(true); 96 } 97 98 public static class ComboPopupCheckListener implements PopupMenuListener { 99 100 public void popupMenuCanceled(PopupMenuEvent ev) { 101 } 102 103 public void popupMenuWillBecomeVisible(PopupMenuEvent ev) { 104 } 105 106 public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) { 107 Point cpos = combo1.getLocation(); 108 SwingUtilities.convertPointToScreen(cpos, panel); 109 110 JPopupMenu pm = (JPopupMenu) combo1.getUI().getAccessibleChild(combo1, 0); 111 112 if (pm != null) { 113 Point p = pm.getLocation(); 114 SwingUtilities.convertPointToScreen(p, pm); 115 if (p.y < cpos.y) { 116 throw new RuntimeException("ComboBox popup is wrongly aligned"); 117 } // check that popup was opened down 118 } 119 } 120 } 121 122 private class PopupHandler extends AbstractAction { 123 124 public void actionPerformed(ActionEvent e) { 125 if (!popupMenu.isVisible()) { 126 popupMenu.show((Component) e.getSource(), 40, 40); 127 } 128 isPopupOnScreen(popupMenu, fullScreenBounds); 129 } 130 } 131 132 class PopupListener extends MouseAdapter { 133 134 private JPopupMenu popup; 135 136 public PopupListener(JPopupMenu popup) { 137 this.popup = popup; 138 } 139 140 public void mousePressed(MouseEvent e) { 169 } 170 171 private JPanel createContentPane() { 172 JPanel panel = new JPanel(); 173 174 combo1 = new JComboBox<>(numData); 175 panel.add(combo1); 176 combo2 = new JComboBox<>(dayData); 177 combo2.setEditable(true); 178 panel.add(combo2); 179 panel.setSize(300, 200); 180 181 popupMenu = new JPopupMenu(); 182 JMenuItem item; 183 for (int i = 0; i < dayData.length; i++) { 184 item = popupMenu.add(new JMenuItem(dayData[i], mnDayData[i])); 185 item.addActionListener(this); 186 } 187 panel.addMouseListener(new PopupListener(popupMenu)); 188 189 JTextField field = new JTextField("CTRL+down for Popup"); 190 // CTRL-down will show the popup. 191 field.getInputMap().put(KeyStroke.getKeyStroke( 192 KeyEvent.VK_DOWN, InputEvent.CTRL_MASK), "OPEN_POPUP"); 193 field.getActionMap().put("OPEN_POPUP", new PopupHandler()); 194 195 panel.add(field); 196 197 return panel; 198 } 199 200 /** 201 * @param str name of Menu 202 * @param bFlag set mnemonics on menu items 203 */ 204 private JMenuBar createMenuBar(String str, boolean bFlag) { 205 menubar = new JMenuBar(); 206 207 menu1 = new JMenu(str); 208 menu1.setMnemonic(str.charAt(0)); 209 menu1.addActionListener(this); 210 211 menubar.add(menu1); 212 for (int i = 0; i < 8; i++) { 243 menuitem.setMnemonic('0' + i); 244 } 245 submenu.add(menuitem); 246 } 247 menu2.add(new JSeparator()); 248 menu2.add(submenu); 249 250 return menubar; 251 } 252 253 public void actionPerformed(ActionEvent evt) { 254 Object obj = evt.getSource(); 255 if (obj instanceof JMenuItem) { 256 // put the focus on the noneditable combo. 257 combo1.requestFocus(); 258 } 259 } 260 261 public static void main(String[] args) throws Throwable { 262 263 264 SwingUtilities.invokeAndWait(new Runnable() { 265 public void run() { 266 test = new TaskbarPositionTest(); 267 } 268 }); 269 270 // Use Robot to automate the test 271 Robot robot; 272 robot = new Robot(); 273 robot.setAutoDelay(125); 274 275 // 1 - menu 276 Util.hitMnemonics(robot, KeyEvent.VK_1); 277 278 robot.waitForIdle(); 279 isPopupOnScreen(menu1.getPopupMenu(), screenBounds); 280 281 // 2 menu with sub menu 282 robot.keyPress(KeyEvent.VK_RIGHT); 283 robot.keyRelease(KeyEvent.VK_RIGHT); 284 Util.hitMnemonics(robot, KeyEvent.VK_S); 285 286 robot.waitForIdle(); 287 isPopupOnScreen(menu2.getPopupMenu(), screenBounds); 288 289 robot.keyPress(KeyEvent.VK_ENTER); 290 robot.keyRelease(KeyEvent.VK_ENTER); 291 292 // Focus should go to non editable combo box 293 robot.waitForIdle(); 294 Thread.sleep(500); 295 296 robot.keyPress(KeyEvent.VK_DOWN); 297 298 // How do we check combo boxes? 299 300 // Editable combo box 301 robot.keyPress(KeyEvent.VK_TAB); 302 robot.keyRelease(KeyEvent.VK_TAB); 303 robot.keyPress(KeyEvent.VK_DOWN); 304 robot.keyRelease(KeyEvent.VK_DOWN); 305 306 // combo1.getUI(); 307 308 // Popup from Text field 309 robot.keyPress(KeyEvent.VK_TAB); 310 robot.keyRelease(KeyEvent.VK_TAB); 311 robot.keyPress(KeyEvent.VK_CONTROL); 312 robot.keyPress(KeyEvent.VK_DOWN); 313 robot.keyRelease(KeyEvent.VK_DOWN); 314 robot.keyRelease(KeyEvent.VK_CONTROL); 315 316 // Popup from a mouse click. 317 Point pt = new Point(2, 2); 318 SwingUtilities.convertPointToScreen(pt, panel); 319 robot.mouseMove((int) pt.getX(), (int) pt.getY()); 320 robot.mousePress(InputEvent.BUTTON3_MASK); 321 robot.mouseRelease(InputEvent.BUTTON3_MASK); 322 323 robot.waitForIdle(); 324 SwingUtilities.invokeAndWait(new Runnable() { 325 public void run() { 326 test.setLocation(-30, 100); 327 combo1.addPopupMenuListener(new ComboPopupCheckListener()); 328 combo1.requestFocus(); 329 } 330 }); 331 332 robot.keyPress(KeyEvent.VK_DOWN); 333 robot.keyRelease(KeyEvent.VK_DOWN); 334 robot.keyPress(KeyEvent.VK_ESCAPE); 335 robot.keyRelease(KeyEvent.VK_ESCAPE); 336 337 robot.waitForIdle(); 338 Thread.sleep(500); 339 } 340 } | 45 private static JPanel panel; 46 private static JComboBox<String> combo1; 47 private static JComboBox<String> combo2; 48 private static JMenuBar menubar; 49 private static JMenu menu1; 50 private static JMenu menu2; 51 private static Rectangle fullScreenBounds; 52 // The usable desktop space: screen size - screen insets. 53 private static Rectangle screenBounds; 54 private static String[] numData = { 55 "One", "Two", "Three", "Four", "Five", "Six", "Seven" 56 }; 57 private static String[] dayData = { 58 "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" 59 }; 60 private static char[] mnDayData = { 61 'M', 'T', 'W', 'R', 'F', 'S', 'U' 62 }; 63 64 public TaskbarPositionTest() { 65 super("Use down to show a JPopupMenu"); 66 setContentPane(panel = createContentPane()); 67 setJMenuBar(createMenuBar("1 - First Menu", true)); 68 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 69 70 //Down will show the popup. 71 panel.getInputMap().put(KeyStroke.getKeyStroke( 72 KeyEvent.VK_DOWN, 0), "OPEN_POPUP"); 73 panel.getActionMap().put("OPEN_POPUP", new PopupHandler()); 74 75 pack(); 76 77 Toolkit toolkit = Toolkit.getDefaultToolkit(); 78 fullScreenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); 79 screenBounds = new Rectangle(new Point(), toolkit.getScreenSize()); 80 81 // Place the frame near the bottom. This is a pretty wild guess. 82 this.setLocation(0, (int) screenBounds.getHeight() - 2 * this.getHeight()); 83 84 // Reduce the screen bounds by the insets. 85 GraphicsConfiguration gc = this.getGraphicsConfiguration(); 86 if (gc != null) { 87 Insets screenInsets = toolkit.getScreenInsets(gc); 88 screenBounds = gc.getBounds(); 89 screenBounds.width -= (screenInsets.left + screenInsets.right); 90 screenBounds.height -= (screenInsets.top + screenInsets.bottom); 91 screenBounds.x += screenInsets.left; 92 screenBounds.y += screenInsets.top; 95 setVisible(true); 96 } 97 98 public static class ComboPopupCheckListener implements PopupMenuListener { 99 100 public void popupMenuCanceled(PopupMenuEvent ev) { 101 } 102 103 public void popupMenuWillBecomeVisible(PopupMenuEvent ev) { 104 } 105 106 public void popupMenuWillBecomeInvisible(PopupMenuEvent ev) { 107 Point cpos = combo1.getLocation(); 108 SwingUtilities.convertPointToScreen(cpos, panel); 109 110 JPopupMenu pm = (JPopupMenu) combo1.getUI().getAccessibleChild(combo1, 0); 111 112 if (pm != null) { 113 Point p = pm.getLocation(); 114 SwingUtilities.convertPointToScreen(p, pm); 115 if (!UIManager.getLookAndFeel() 116 .getName().toLowerCase().contains("os x")) { 117 if (p.y < cpos.y) { 118 throw new RuntimeException("ComboBox popup is wrongly aligned"); 119 } // check that popup was opened down 120 } 121 } 122 } 123 } 124 125 private class PopupHandler extends AbstractAction { 126 127 public void actionPerformed(ActionEvent e) { 128 if (!popupMenu.isVisible()) { 129 popupMenu.show((Component) e.getSource(), 40, 40); 130 } 131 isPopupOnScreen(popupMenu, fullScreenBounds); 132 } 133 } 134 135 class PopupListener extends MouseAdapter { 136 137 private JPopupMenu popup; 138 139 public PopupListener(JPopupMenu popup) { 140 this.popup = popup; 141 } 142 143 public void mousePressed(MouseEvent e) { 172 } 173 174 private JPanel createContentPane() { 175 JPanel panel = new JPanel(); 176 177 combo1 = new JComboBox<>(numData); 178 panel.add(combo1); 179 combo2 = new JComboBox<>(dayData); 180 combo2.setEditable(true); 181 panel.add(combo2); 182 panel.setSize(300, 200); 183 184 popupMenu = new JPopupMenu(); 185 JMenuItem item; 186 for (int i = 0; i < dayData.length; i++) { 187 item = popupMenu.add(new JMenuItem(dayData[i], mnDayData[i])); 188 item.addActionListener(this); 189 } 190 panel.addMouseListener(new PopupListener(popupMenu)); 191 192 JTextField field = new JTextField("Press down for Popup"); 193 //Down will show the popup. 194 field.getInputMap().put(KeyStroke.getKeyStroke( 195 KeyEvent.VK_DOWN, 0), "OPEN_POPUP"); 196 field.getActionMap().put("OPEN_POPUP", new PopupHandler()); 197 198 panel.add(field); 199 200 return panel; 201 } 202 203 /** 204 * @param str name of Menu 205 * @param bFlag set mnemonics on menu items 206 */ 207 private JMenuBar createMenuBar(String str, boolean bFlag) { 208 menubar = new JMenuBar(); 209 210 menu1 = new JMenu(str); 211 menu1.setMnemonic(str.charAt(0)); 212 menu1.addActionListener(this); 213 214 menubar.add(menu1); 215 for (int i = 0; i < 8; i++) { 246 menuitem.setMnemonic('0' + i); 247 } 248 submenu.add(menuitem); 249 } 250 menu2.add(new JSeparator()); 251 menu2.add(submenu); 252 253 return menubar; 254 } 255 256 public void actionPerformed(ActionEvent evt) { 257 Object obj = evt.getSource(); 258 if (obj instanceof JMenuItem) { 259 // put the focus on the noneditable combo. 260 combo1.requestFocus(); 261 } 262 } 263 264 public static void main(String[] args) throws Throwable { 265 266 SwingUtilities.invokeAndWait(new Runnable() { 267 public void run() { 268 test = new TaskbarPositionTest(); 269 } 270 }); 271 272 // Use Robot to automate the test 273 Robot robot; 274 robot = new Robot(); 275 robot.setAutoDelay(125); 276 277 // 1 - menu 278 Util.hitMnemonics(robot, KeyEvent.VK_1); 279 280 robot.waitForIdle(); 281 isPopupOnScreen(menu1.getPopupMenu(), screenBounds); 282 283 // 2 menu with sub menu 284 robot.keyPress(KeyEvent.VK_RIGHT); 285 robot.keyRelease(KeyEvent.VK_RIGHT); 286 Util.hitMnemonics(robot, KeyEvent.VK_S); 287 288 robot.waitForIdle(); 289 isPopupOnScreen(menu2.getPopupMenu(), screenBounds); 290 291 robot.keyPress(KeyEvent.VK_ENTER); 292 robot.keyRelease(KeyEvent.VK_ENTER); 293 294 // Focus should go to non editable combo box 295 robot.waitForIdle(); 296 Thread.sleep(500); 297 298 robot.keyPress(KeyEvent.VK_DOWN); 299 300 // How do we check combo boxes? 301 // Editable combo box 302 robot.keyPress(KeyEvent.VK_TAB); 303 robot.keyRelease(KeyEvent.VK_TAB); 304 robot.keyPress(KeyEvent.VK_DOWN); 305 robot.keyRelease(KeyEvent.VK_DOWN); 306 307 // combo1.getUI(); 308 // Popup from Text field 309 robot.keyPress(KeyEvent.VK_TAB); 310 robot.keyRelease(KeyEvent.VK_TAB); 311 robot.keyPress(KeyEvent.VK_DOWN); 312 robot.keyRelease(KeyEvent.VK_DOWN); 313 314 // Popup from a mouse click. 315 Point pt = new Point(2, 2); 316 SwingUtilities.convertPointToScreen(pt, panel); 317 robot.mouseMove((int) pt.getX(), (int) pt.getY()); 318 robot.mousePress(InputEvent.BUTTON3_MASK); 319 robot.mouseRelease(InputEvent.BUTTON3_MASK); 320 321 robot.waitForIdle(); 322 SwingUtilities.invokeAndWait(new Runnable() { 323 public void run() { 324 test.setLocation(-30, 100); 325 combo1.addPopupMenuListener(new ComboPopupCheckListener()); 326 combo1.requestFocus(); 327 } 328 }); 329 330 robot.keyPress(KeyEvent.VK_DOWN); 331 robot.keyRelease(KeyEvent.VK_DOWN); 332 robot.keyPress(KeyEvent.VK_ESCAPE); 333 robot.keyRelease(KeyEvent.VK_ESCAPE); 334 335 robot.waitForIdle(); 336 Thread.sleep(500); 337 disposeGUI(); 338 } 339 340 private static void disposeGUI() throws Exception { 341 SwingUtilities.invokeAndWait(new Runnable() { 342 public void run() { 343 test.dispose(); 344 } 345 }); 346 } 347 } 348 |