📄 basiccombopopup.java
字号:
// TODO: We need to handle the shouldScroll parameter somehow. int index = list.locationToIndex(anEvent.getPoint()); // Check for valid index. if (index >= 0) list.setSelectedIndex(index); } /** * InvocationMouseHandler is a listener that listens to mouse events * occuring in the combo box. Note that this listener doesn't listen to * mouse events occuring in the popup portion of the combo box, it only * listens to main combo box part(area that displays selected item). This * listener is responsible for showing and hiding popup portion of the * combo box. */ protected class InvocationMouseHandler extends MouseAdapter { /** * Creates a new InvocationMouseHandler object. */ protected InvocationMouseHandler() { // Nothing to do here. } /** * This method is invoked whenever mouse is being pressed over the main * part of the combo box. This method will show popup if the popup is * not shown on the screen right now, and it will hide popup otherwise. * * @param e MouseEvent that should be handled */ public void mousePressed(MouseEvent e) { if (comboBox.isEnabled()) togglePopup(); } /** * This method is invoked whenever mouse event was originated in the combo * box and released either in the combBox list of items or in the combo * box itself. * * @param e MouseEvent that should be handled */ public void mouseReleased(MouseEvent e) { // Get component over which mouse was released Component src = (Component) e.getSource(); int x = e.getX(); int y = e.getY(); Component releasedComponent = SwingUtilities.getDeepestComponentAt(src, x, y); // if mouse was released inside the bounds of combo box then do nothing, // Otherwise if mouse was released inside the list of combo box items // then change selection and close popup if (! (releasedComponent instanceof JComboBox)) { // List model contains the item over which mouse is released, // since it is updated every time the mouse is moved over a different // item in the list. Now that the mouse is released we need to // update model of the combo box as well. comboBox.setSelectedIndex(list.getSelectedIndex()); if (isAutoScrolling) stopAutoScrolling(); hide(); } } } /** * InvocationMouseMotionListener is a mouse listener that listens to mouse * dragging events occuring in the combo box. */ protected class InvocationMouseMotionHandler extends MouseMotionAdapter { /** * Creates a new InvocationMouseMotionHandler object. */ protected InvocationMouseMotionHandler() { // Nothing to do here. } /** * This method is responsible for highlighting item in the drop down list * over which the mouse is currently being dragged. */ public void mouseDragged(MouseEvent e) { // convert point of the drag event relative to combo box list component // figure out over which list cell the mouse is currently being dragged // and highlight the cell. The list model is changed but the change has // no effect on combo box's data model. The list model is changed so // that the appropriate item would be highlighted in the combo box's // list. if (BasicComboPopup.this.isVisible()) { int cbHeight = (int) comboBox.getPreferredSize().getHeight(); int popupHeight = BasicComboPopup.this.getSize().height; // if mouse is dragged inside the the combo box's items list. if (e.getY() > cbHeight && ! (e.getY() - cbHeight >= popupHeight)) { int index = list.locationToIndex(new Point(e.getX(), (int) (e.getY() - cbHeight))); int firstVisibleIndex = list.getFirstVisibleIndex(); // list.locationToIndex returns item's index that would // be located at the specified point if the first item that // is visible is item 0. However in the JComboBox it is not // necessarily the case since list is contained in the // JScrollPane so we need to adjust the index returned. if (firstVisibleIndex != 0) // FIXME: adjusted index here is off by one. I am adding one // here to compensate for that. This should be // index += firstVisibleIndex. Remove +1 once the bug is fixed. index += firstVisibleIndex + 1; list.setSelectedIndex(index); } else { // if mouse is being dragged at the bottom of combo box's list // of items or at the very top then scroll the list in the // desired direction. boolean movingUP = e.getY() < cbHeight; boolean movingDown = e.getY() > cbHeight; if (movingUP) { scrollDirection = SCROLL_UP; startAutoScrolling(SCROLL_UP); } else if (movingDown) { scrollDirection = SCROLL_DOWN; startAutoScrolling(SCROLL_DOWN); } } } } } /** * ItemHandler is an item listener that listens to selection events occuring * in the combo box. FIXME: should specify here what it does when item is * selected or deselected in the combo box list. */ protected class ItemHandler extends Object implements ItemListener { /** * Creates a new ItemHandler object. */ protected ItemHandler() { // Nothing to do here. } /** * This method responds to the selection events occuring in the combo box. * * @param e ItemEvent specifying the combo box's selection */ public void itemStateChanged(ItemEvent e) { // TODO: What should be done here? } } /** * ListMouseHandler is a listener that listens to mouse events occuring in * the combo box's list of items. This class is responsible for hiding * popup portion of the combo box if the mouse is released inside the combo * box's list. */ protected class ListMouseHandler extends MouseAdapter { protected ListMouseHandler() { // Nothing to do here. } public void mousePressed(MouseEvent e) { // TODO: What should be do here? } public void mouseReleased(MouseEvent anEvent) { int index = list.locationToIndex(anEvent.getPoint()); // Check for valid index. if (index >= 0) comboBox.setSelectedIndex(index); hide(); } } /** * ListMouseMotionHandler listens to mouse motion events occuring in the * combo box's list. This class is responsible for highlighting items in * the list when mouse is moved over them */ protected class ListMouseMotionHandler extends MouseMotionAdapter { protected ListMouseMotionHandler() { // Nothing to do here. } public void mouseMoved(MouseEvent anEvent) { updateListBoxSelectionForEvent(anEvent, false); } } /** * This class listens to changes occuring in the bound properties of the * combo box */ protected class PropertyChangeHandler extends Object implements PropertyChangeListener { protected PropertyChangeHandler() { // Nothing to do here. } public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName().equals("renderer")) { list.setCellRenderer((ListCellRenderer) e.getNewValue()); revalidate(); repaint(); } if (e.getPropertyName().equals("dataModel")) { list.setModel((ComboBoxModel) e.getNewValue()); revalidate(); repaint(); } } } // ------ private helper methods -------------------- /** * This method uninstalls listeners installed by the UI */ private void uninstallListeners() { uninstallListListeners(); uninstallComboBoxListeners(); uninstallComboBoxModelListeners(comboBox.getModel()); } /** * This method uninstalls Listeners registered with combo boxes list of * items */ private void uninstallListListeners() { list.removeMouseListener(listMouseListener); listMouseListener = null; list.removeMouseMotionListener(listMouseMotionListener); listMouseMotionListener = null; } /** * This method uninstalls listeners listening to combo box associated with * this popup menu */ private void uninstallComboBoxListeners() { comboBox.removeMouseListener(mouseListener); mouseListener = null; comboBox.removeMouseMotionListener(mouseMotionListener); mouseMotionListener = null; comboBox.removeItemListener(itemListener); itemListener = null; comboBox.removePropertyChangeListener(propertyChangeListener); propertyChangeListener = null; } // -------------------------------------------------------------------- // The following classes are here only for backwards API compatibility // They aren't used. // -------------------------------------------------------------------- /** * This class is not used any more. */ public class ListDataHandler extends Object implements ListDataListener { public ListDataHandler() { // Nothing to do here. } public void contentsChanged(ListDataEvent e) { // Nothing to do here. } public void intervalAdded(ListDataEvent e) { // Nothing to do here. } public void intervalRemoved(ListDataEvent e) { // Nothing to do here. } } /** * This class is not used anymore */ protected class ListSelectionHandler extends Object implements ListSelectionListener { protected ListSelectionHandler() { // Nothing to do here. } public void valueChanged(ListSelectionEvent e) { // Nothing to do here. } } /** * This class is not used anymore */ public class InvocationKeyHandler extends KeyAdapter { public InvocationKeyHandler() { // Nothing to do here. } public void keyReleased(KeyEvent e) { // Nothing to do here. } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -