📄 basiccombopopup.java
字号:
* <code>ComboBoxModel</code>. If this method returns null then * it will not be added to the combo box model. * * @return an instance of a <code>ListDataListener</code> or null */ protected ListDataListener createListDataListener() { return null; } /** * Creates a mouse listener that watches for mouse events in * the popup's list. If this method returns null then it will * not be added to the combo box. * * @return an instance of a <code>MouseListener</code> or null */ protected MouseListener createListMouseListener() { return new ListMouseHandler(); } /** * Creates a mouse motion listener that watches for mouse motion * events in the popup's list. If this method returns null then it will * not be added to the combo box. * * @return an instance of a <code>MouseMotionListener</code> or null */ protected MouseMotionListener createListMouseMotionListener() { return new ListMouseMotionHandler(); } /** * Creates a <code>PropertyChangeListener</code> which will be added to * the combo box. If this method returns null then it will not * be added to the combo box. * * @return an instance of a <code>PropertyChangeListener</code> or null */ protected PropertyChangeListener createPropertyChangeListener() { return new PropertyChangeHandler(); } /** * Creates an <code>ItemListener</code> which will be added to the * combo box. If this method returns null then it will not * be added to the combo box. * <p> * Subclasses may override this method to return instances of their own * ItemEvent handlers. * * @return an instance of an <code>ItemListener</code> or null */ protected ItemListener createItemListener() { return new ItemHandler(); } /** * Creates the JList used in the popup to display * the items in the combo box model. This method is called when the UI class * is created. * * @return a <code>JList</code> used to display the combo box items */ protected JList createList() { return new JList( comboBox.getModel() ) { public void processMouseEvent(MouseEvent e) { if (e.isControlDown()) { // Fix for 4234053. Filter out the Control Key from the list. // ie., don't allow CTRL key deselection. e = new MouseEvent((Component)e.getSource(), e.getID(), e.getWhen(), e.getModifiers() ^ InputEvent.CTRL_MASK, e.getX(), e.getY(), e.getClickCount(), e.isPopupTrigger()); } super.processMouseEvent(e); } }; } /** * Configures the list which is used to hold the combo box items in the * popup. This method is called when the UI class * is created. * * @see #createList */ protected void configureList() { list.setFont( comboBox.getFont() ); list.setForeground( comboBox.getForeground() ); list.setBackground( comboBox.getBackground() ); list.setSelectionForeground( UIManager.getColor( "ComboBox.selectionForeground" ) ); list.setSelectionBackground( UIManager.getColor( "ComboBox.selectionBackground" ) ); list.setBorder( null ); list.setCellRenderer( comboBox.getRenderer() ); list.setFocusable( false ); list.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); setListSelection( comboBox.getSelectedIndex() ); installListListeners(); } /** * Adds the listeners to the list control. */ protected void installListListeners() { if ((listMouseListener = createListMouseListener()) != null) { list.addMouseListener( listMouseListener ); } if ((listMouseMotionListener = createListMouseMotionListener()) != null) { list.addMouseMotionListener( listMouseMotionListener ); } if ((listSelectionListener = createListSelectionListener()) != null) { list.addListSelectionListener( listSelectionListener ); } } void uninstallListListeners() { if (listMouseListener != null) { list.removeMouseListener(listMouseListener); listMouseListener = null; } if (listMouseMotionListener != null) { list.removeMouseMotionListener(listMouseMotionListener); listMouseMotionListener = null; } if (listSelectionListener != null) { list.removeListSelectionListener(listSelectionListener); listSelectionListener = null; } } /** * Creates the scroll pane which houses the scrollable list. */ protected JScrollPane createScroller() { return new JScrollPane( list, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER ); } /** * Configures the scrollable portion which holds the list within * the combo box popup. This method is called when the UI class * is created. */ protected void configureScroller() { scroller.setFocusable( false ); scroller.getVerticalScrollBar().setFocusable( false ); scroller.setBorder( null ); } /** * Configures the popup portion of the combo box. This method is called * when the UI class is created. */ protected void configurePopup() { setLayout( new BoxLayout( this, BoxLayout.Y_AXIS ) ); setBorderPainted( true ); setBorder( BorderFactory.createLineBorder( Color.black ) ); setOpaque( false ); add( scroller ); setDoubleBuffered( true ); setFocusable( false ); } /** * This method adds the necessary listeners to the JComboBox. */ protected void installComboBoxListeners() { if ((propertyChangeListener = createPropertyChangeListener()) != null) { comboBox.addPropertyChangeListener(propertyChangeListener); } if ((itemListener = createItemListener()) != null) { comboBox.addItemListener(itemListener); } installComboBoxModelListeners(comboBox.getModel()); } /** * Installs the listeners on the combo box model. Any listeners installed * on the combo box model should be removed in * <code>uninstallComboBoxModelListeners</code>. * * @param model The combo box model to install listeners * @see #uninstallComboBoxModelListeners */ protected void installComboBoxModelListeners( ComboBoxModel model ) { if (model != null && (listDataListener = createListDataListener()) != null) { model.addListDataListener(listDataListener); } } protected void installKeyboardActions() { /* XXX - shouldn't call this method. take it out for testing. ActionListener action = new ActionListener() { public void actionPerformed(ActionEvent e){ } }; comboBox.registerKeyboardAction( action, KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); */ } // // end Initialization routines //================================================================= //=================================================================== // begin Event Listenters // /** * A listener to be registered upon the combo box * (<em>not</em> its popup menu) * to handle mouse events * that affect the state of the popup menu. * The main purpose of this listener is to make the popup menu * appear and disappear. * This listener also helps * with click-and-drag scenarios by setting the selection if the mouse was * released over the list during a drag. * * <p> * <strong>Warning:</strong> * We recommend that you <em>not</em> * create subclasses of this class. * If you absolutely must create a subclass, * be sure to invoke the superclass * version of each method. * * @see BasicComboPopup#createMouseListener */ protected class InvocationMouseHandler extends MouseAdapter { /** * Responds to mouse-pressed events on the combo box. * * @param e the mouse-press event to be handled */ public void mousePressed( MouseEvent e ) { if (!SwingUtilities.isLeftMouseButton(e) || !comboBox.isEnabled()) return; if ( comboBox.isEditable() ) { Component comp = comboBox.getEditor().getEditorComponent(); if ((!(comp instanceof JComponent)) || ((JComponent)comp).isRequestFocusEnabled()) { comp.requestFocus(); } } else if (comboBox.isRequestFocusEnabled()) { comboBox.requestFocus(); } togglePopup(); } /** * Responds to the user terminating * a click or drag that began on the combo box. * * @param e the mouse-release event to be handled */ public void mouseReleased( MouseEvent e ) { Component source = (Component)e.getSource(); Dimension size = source.getSize(); Rectangle bounds = new Rectangle( 0, 0, size.width - 1, size.height - 1 ); if ( !bounds.contains( e.getPoint() ) ) { MouseEvent newEvent = convertMouseEvent( e ); Point location = newEvent.getPoint(); Rectangle r = new Rectangle(); list.computeVisibleRect( r ); if ( r.contains( location ) ) { comboBox.setSelectedIndex( list.getSelectedIndex() ); } comboBox.setPopupVisible(false); } hasEntered = false; stopAutoScrolling(); } } /** * This listener watches for dragging and updates the current selection in the * list if it is dragging over the list. */ protected class InvocationMouseMotionHandler extends MouseMotionAdapter { public void mouseDragged( MouseEvent e ) { if ( isVisible() ) { MouseEvent newEvent = convertMouseEvent( e ); Rectangle r = new Rectangle(); list.computeVisibleRect( r ); if ( newEvent.getPoint().y >= r.y && newEvent.getPoint().y <= r.y + r.height - 1 ) { hasEntered = true; if ( isAutoScrolling ) { stopAutoScrolling(); } Point location = newEvent.getPoint(); if ( r.contains( location ) ) { updateListBoxSelectionForEvent( newEvent, false ); } } else { if ( hasEntered ) { int directionToScroll = newEvent.getPoint().y < r.y ? SCROLL_UP : SCROLL_DOWN; if ( isAutoScrolling && scrollDirection != directionToScroll ) { stopAutoScrolling(); startAutoScrolling( directionToScroll ); } else if ( !isAutoScrolling ) { startAutoScrolling( directionToScroll ); } } else { if ( e.getPoint().y < 0 ) { hasEntered = true; startAutoScrolling( SCROLL_UP ); } } } } } } /** * As of Java 2 platform v 1.4, this class is now obsolete and is only included for * backwards API compatibility. Do not instantiate or subclass. * <p> * All the functionality of this class has been included in * BasicComboBoxUI ActionMap/InputMap methods. */ public class InvocationKeyHandler extends KeyAdapter { public void keyReleased( KeyEvent e ) {} } /** * As of Java 2 platform v 1.4, this class is now obsolete, doesn't do anything, and * is only included for backwards API compatibility. Do not call or * override. */ protected class ListSelectionHandler implements ListSelectionListener { public void valueChanged( ListSelectionEvent e ) {} } /** * As of 1.4, this class is now obsolete, doesn't do anything, and * is only included for backwards API compatibility. Do not call or * override. * <p> * The functionality has been migrated into <code>ItemHandler</code>. * * @see #createItemListener */ public class ListDataHandler implements ListDataListener { public void contentsChanged( ListDataEvent e ) {} public void intervalAdded( ListDataEvent e ) { } public void intervalRemoved( ListDataEvent e ) { } } /** * This listener hides the popup when the mouse is released in the list. */ protected class ListMouseHandler extends MouseAdapter { public void mousePressed( MouseEvent e ) { } public void mouseReleased(MouseEvent anEvent) { comboBox.setSelectedIndex( list.getSelectedIndex() ); comboBox.setPopupVisible(false); // workaround for cancelling an edited item (bug 4530953) if (comboBox.isEditable() && comboBox.getEditor() != null) { comboBox.configureEditor(comboBox.getEditor(), comboBox.getSelectedItem()); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -