⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basiccombopopup.java

📁 JAVA 所有包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * 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 getHandler();    }    /**     * 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 getHandler();    }    private Handler getHandler() {        if (handler == null) {            handler = new Handler();        }        return handler;    }    /**     * 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.getXOnScreen(), e.getYOnScreen(),                                       e.getClickCount(),                                       e.isPopupTrigger(),                                       MouseEvent.NOBUTTON);                }                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;	}        handler = null;    }    /**     * Creates the scroll pane which houses the scrollable list.     */    protected JScrollPane createScroller() {        JScrollPane sp = new JScrollPane( list, 				ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER );        sp.setHorizontalScrollBar(null);        return sp;    }    /**     * 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(LIST_BORDER);        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 ) {            getHandler().mousePressed(e);        }	/**	 * 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 ) {            getHandler().mouseReleased(e);        }    }    /**     * 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 ) {            getHandler().mouseDragged(e);        }    }    /**     * 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) {            getHandler().mouseReleased(anEvent);        }    }    /**     * This listener changes the selected item as you move the mouse over the list.     * The selection change is not committed to the model, this is for user feedback only.     */    protected class ListMouseMotionHandler extends MouseMotionAdapter {        public void mouseMoved( MouseEvent anEvent ) {            getHandler().mouseMoved(anEvent);        }    }    /**     * This listener watches for changes to the selection in the      * combo box.     */    protected class ItemHandler implements ItemListener {        public void itemStateChanged( ItemEvent e ) {            getHandler().itemStateChanged(e);	}    }    /**     * This listener watches for bound properties that have changed in the     * combo box.      * <p>     * Subclasses which wish to listen to combo box property changes should     * call the superclass methods to ensure that the combo popup correctly      * handles property changes.     *      * @see #createPropertyChangeListener     */    protected class PropertyChangeHandler implements PropertyChangeListener {        public void propertyChange( PropertyChangeEvent e ) {            getHandler().propertyChange(e);        }    }    private class AutoScrollActionHandler implements ActionListener {        private int direction;        AutoScrollActionHandler(int direction) {            this.direction = direction;        }        public void actionPerformed(ActionEvent e) {            if (direction == SCROLL_UP) {                autoScrollUp();            }            else {                autoScrollDown();            }        }    }    private class Handler implements ItemListener, MouseListener,                          MouseMotionListener, PropertyChangeListener,                          Serializable {        //        // MouseListener        // NOTE: this is added to both the JList and JComboBox        //        public void mouseClicked(MouseEvent e) {        }        public void mousePressed(MouseEvent e) {            if (e.getSource() == list) {                return;            }            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();        }        public void mouseReleased(MouseEvent e) {            if (e.getSource() == list) {                if (list.getModel().getSize() > 0) {                    // JList mouse listener                    if (comboBox.getSelectedIndex() != list.getSelectedIndex()) {                        comboBox.setSelectedIndex( list.getSelectedIndex() );                    } else {                        comboBox.getEditor().setItem( list.getSelectedValue() );                    }                }                comboBox.setPopupVisible(false);                // workaround for cancelling an edited item (bug 4530953)                if (comboBox.isEditable() && comboBox.getEditor() != null) {                    comboBox.configureEditor(comboBox.getEditor(),                                              comboBox.getSelectedItem());                 }                return;            }            // JComboBox mouse listener	    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 ) ) {                    if (comboBox.getSelectedIndex() != list.getSelectedIndex()) {                        comboBox.setSelectedIndex( list.getSelectedIndex() );                    } else {                        comboBox.getEditor().setItem( list.getSelectedValue() );                    }                }		comboBox.setPopupVisible(false);            }            hasEntered = false;            stopAutoScrolling();        }        public void mouseEntered(MouseEvent e) {        }        public void mouseExited(MouseEvent e) {        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -