private void processKeyPressedWhenSuggestionListIsVisible(KeyEvent e) { int sel = suggestionList.getSelectedIndex(); int max = suggestionList.getItemCount() - 1; int next; switch (e.getKeyCode()) { case KeyEvent.VK_UP: case KeyEvent.VK_NUMPAD8: next = sel - 1; break; case KeyEvent.VK_DOWN: case KeyEvent.VK_NUMPAD2: next = sel + 1; break; case KeyEvent.VK_PAGE_UP: next = sel - comboBox.getMaximumRowCount(); break; case KeyEvent.VK_PAGE_DOWN: next = sel + comboBox.getMaximumRowCount(); break; case KeyEvent.VK_HOME: next = 0; break; case KeyEvent.VK_END: next = max; break; case KeyEvent.VK_ENTER: processEnterPressed(); return; case KeyEvent.VK_ESCAPE: suggestionList.hide(); return; default: // invoke in end of AWT thread so that information in textEditor would update SwingUtilities.invokeLater(this); return; } e.consume(); handleNavigationKeys(true, next, sel, max); }
/** * Constructor. * * @param combo combobox reference */ ComboPopup(final JComboBox combo) { super(combo); final int h = combo.getMaximumRowCount(); setPreferredSize(new Dimension(getPreferredSize().width, getPopupHeightForRowCount(h) + 2)); }
/* * I can't find any property on the scrollBar to determine if it will be * displayed or not so use brute force to determine this. */ protected int getScrollBarWidth(BasicComboPopup popup, JScrollPane scrollPane) { int scrollBarWidth = 0; JComboBox comboBox = (JComboBox) popup.getInvoker(); if (comboBox.getItemCount() > comboBox.getMaximumRowCount()) { JScrollBar vertical = scrollPane.getVerticalScrollBar(); scrollBarWidth = vertical.getPreferredSize().width; } return scrollBarWidth; }