@Override public void keyPressed(KeyEvent evt) { if (evt.isConsumed()) return; Component comp = getFocusOwner(); if (evt.getKeyCode() == KeyEvent.VK_ENTER && enterEnabled) { while (comp != null) { if (comp instanceof JComboBox) { JComboBox<?> combo = (JComboBox<?>) comp; if (combo.isEditable()) { Object selected = combo.getEditor().getItem(); if (selected != null) combo.setSelectedItem(selected); } if (combo.isPopupVisible()) { evt.consume(); combo.setPopupVisible(false); } return; } // TODO: add other classes that need custom key handling here. comp = comp.getParent(); } evt.consume(); ok(); } else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE || isCloseBufferShortcut(evt)) { evt.consume(); if (comp instanceof JComboBox) { JComboBox<?> combo = (JComboBox<?>) comp; if (combo.isPopupVisible()) combo.setPopupVisible(false); else cancel(); } else cancel(); } }
private void handleNavigationKeys(boolean suggest, int next, int sel, int max) { if (!suggest && !comboBox.isPopupVisible()) { comboBox.setPopupVisible(true); return; } if (comboBox.getItemCount() > 0) { if (next < 0) { next = 0; } if (next > max) { next = max; } if (next != sel) { if (suggest) { suggestionList.setSelectedIndex(next); } else { comboBox.setPopupVisible(true); comboBox.setSelectedIndex(next); } } textEditor.requestFocus(); } }
@Test public void test() throws AWTException, InvocationTargetException, InterruptedException { Main window = new Main(); waitForSwing(); JComboBox<String> combo = window.getCombo(); Dimension comboSizes = combo.getSize(); Rectangle comboBounds = combo.getBounds(); Point windowLocation = window.getLocation(); Point comboLocation = combo.getLocation(); Rectangle realBounds = new Rectangle( (int) comboBounds.getX(), (int) comboBounds.getY(), (int) comboSizes.getWidth(), (int) comboSizes.getHeight()); assertEquals(realBounds, comboBounds); int posX = (int) (windowLocation.getX() + comboLocation.getX() + realBounds.getWidth()) - 1; int posY = (int) (windowLocation.getY() + comboLocation.getY() + realBounds.getHeight()) - 1; System.out.println("Window location: " + windowLocation); System.out.println("Combo location: " + comboLocation); System.out.println("Real Bounds: " + realBounds); System.out.println("Clicking at position: " + posX + " " + posY); // Click into the corner... Robot bot = new Robot(); bot.mouseMove(posX, posY); bot.mousePress(InputEvent.BUTTON1_MASK); System.err.println("Mouse pressed."); waitForSwing(); // While we hold down the mouse, the popup should be open assertTrue(combo.isPopupVisible()); // If we release the mouse, it should still open... bot.mouseMove(posX, posY); // maybe the mouse got moved while the test was running bot.mouseRelease(InputEvent.BUTTON1_MASK); System.err.println("Mouse released."); waitForSwing(); assertTrue(combo.isPopupVisible()); }
/** Parse clicks to cancel the recording if we get a click that's not in the JList (or ESC). */ protected boolean parseClick(AWTEvent event) { if (isFinished()) { return false; } // FIXME add key-based activation/termination? boolean consumed = true; if (combo == null) { combo = getComboBox(event); listener = new ActionListener() { public void actionPerformed(ActionEvent ev) { index = combo.getSelectedIndex(); if (!combo.isPopupVisible()) { combo.removeActionListener(listener); setFinished(true); } } }; combo.addActionListener(listener); setStatus("Waiting for selection"); } else if (event.getID() == KeyEvent.KEY_RELEASED && (((KeyEvent) event).getKeyCode() == KeyEvent.VK_SPACE || ((KeyEvent) event).getKeyCode() == KeyEvent.VK_ENTER)) { index = combo.getSelectedIndex(); setFinished(true); } // Cancel via click somewhere else else if (event.getID() == MouseEvent.MOUSE_PRESSED && !AWT.isOnPopup((Component) event.getSource()) && combo != getComboBox(event)) { setFinished(true); consumed = false; } // Cancel via ESC key else if (event.getID() == KeyEvent.KEY_RELEASED && ((KeyEvent) event).getKeyCode() == KeyEvent.VK_ESCAPE) { setStatus("Selection canceled"); setFinished(true); } else { Log.debug("Event ignored"); } if (list == null && combo.isPopupVisible()) list = tester.findComboList(combo); if (isFinished()) { combo.removeActionListener(listener); listener = null; } return consumed; }
public void updateClients() { String message; int online = Query.online(); if (online > 0) message = String.format("Online %d clients", online); else message = "There is not clients online"; field_clients_online.setText(message); ArrayList<Client> list = Query.listOnline(); String[] map = new String[list.size()]; for (int i = 0; i < list.size(); i++) map[i] = String.format("%s", list.get(i).name); if (!combo_clients_online.isPopupVisible()) combo_clients_online.setModel(new DefaultComboBoxModel<String>(map)); }
@Override public void keyPressed(KeyEvent keyEvent) { // don't popup on action keys (cursor movements, etc...) if (keyEvent.isActionKey()) { return; } // don't popup if the combobox isn't visible anyway if (comboBox.isDisplayable() && !comboBox.isPopupVisible()) { int keyCode = keyEvent.getKeyCode(); // don't popup when the user hits shift,ctrl or alt if (keyCode == KeyEvent.VK_SHIFT || keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_ALT) return; // don't popup when the user hits escape (see issue #311) if (keyCode == KeyEvent.VK_ENTER || keyCode == KeyEvent.VK_ESCAPE) return; comboBox.setPopupVisible(true); } }
public boolean canClose() { return !myCombo.isPopupVisible(); }